If I wanted to use the same factory for any class that extended my base class, would I want to turn the base class factory into a named function, or is there a better way to do this?
$serviceManager => array(
'factories' => array(
'someBaseClass' => function($thisServiceManager) {
$db = $thisServiceManager->get('db');
$thisBaseClass = new \myNamespace\thisBaseClass($db);
return $thisBaseClass;
},
),
);
EDIT
Further to the answer I accepted, here's the code I've tested that works.
Class File
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class baseClassFactory implements \Zend\ServiceManager\AbstractFactoryInterface
{
public function canCreateServiceWithName (ServiceLocatorInterface $locator, $name, $requestedName = '')
{
return ('baseClass' === $name || is_subclass_of($name, 'baseClass'));
}
public function createServiceWithName (ServiceLocatorInterface $locator, $name, $requestedName = '')
{
$db = $locator->get('db');
$query = new $name($db);
return $query;
}
}
Configuration
$serviceManager => array(
'abstract_factories' => array(
'baseClassFactory',
),
);