如果我想使用相同的工厂,了解我的扩展基类,我希望把基类的工厂到一个名为功能,还是有更好的方法来做到这一点任何一类?
$serviceManager => array(
'factories' => array(
'someBaseClass' => function($thisServiceManager) {
$db = $thisServiceManager->get('db');
$thisBaseClass = new \myNamespace\thisBaseClass($db);
return $thisBaseClass;
},
),
);
编辑
继我接受了答案,这里的代码我测试过的作品。
类文件
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;
}
}
组态
$serviceManager => array(
'abstract_factories' => array(
'baseClassFactory',
),
);