php - ZF2 Autoloader: Use factory for base class on extended classes as well -
If I increase my base class for any of the squares, then in the base class factory named function, Is a Better Way?
$ serviceManager = & gt; Array ('factory' = & gt; array ('someBaseClass' = & gt; function ($ this service manager) {$ db = $ get this service manager- & gt; get ('db'); $ thisBaseClass = new \ MyNamespace \ thisBaseClass ($ db); return $ thisBaseClass;},)););
Edit
Also for the answer I accepted, here is the code I have worked on.
class file
use Zend \ ServiceManager \ AbstractFactoryInterface; Use Zend \ ServiceManager \ ServiceLocatorInterface; Class base classifier device \ Zend \ ServiceManager \ AbstractFactoryInterface {public function canCreateServiceWithName (ServiceLocatorInterface $ locator, $ name, $ $ Requested name = '') {Return ('baseClass' === $ name || is_subclass_of ($ name, 'baseClass' )); } Public Function createServiceWithName (ServiceLocatorInterface $ locator, $ name, $ requestName = '') {$ db = $ locator- & gt; Obtain ('DB'); $ Query = new $ name ($ db); Return $ query; }}
Configuration
$ serviceManager = & gt; Array ('abstract_factories' = & gt; array ('baseClassFactory',),);
If I follow your question, then you have sections of extensions Is there a bunch someBaseClass
, and do you want them all to be built in the same way?
If so, then it seems like a good opportunity to use the abstract_factories
component in the service manager
will be like a fake code:
// Warning: Untested pseudo-code class MyAbstractFactory tool \ Zend \ ServiceManager \ AbstractFactoryInterface {public function canCreateServiceWithName ($ _, $ _, $ name) {// If I have some basic class or If you have been asked for some basic classes, return the truth: wrong otherwise return ('someBaseClass' === $ name || is_subclass_of ($ name, 'someBaseClass')); } Public function createServiceWithName ($ locator, $ _, $ name) {// Your factory logic is here $ $ object = ... back $ object; }}
and your configuration will look like this:
$ serviceManager = ['abstract_factories' = & gt; ['MyAbstractFactory']];
Using
Comments
Post a Comment