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 factories , you will have to repeat the same function for each extended class (API), which is a pain (IMO) . Originally the essence of above is the factory pattern: Give me a class, I will reflect to see if this is the base class which I meditate about and if so, then I will run my factory to get it.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -