我想在我的自定义代码库中使用 Zend Service Manager。它不是基于 Zend MVC 构建的,而是我只使用一些 Zend 包,如 Zend Console 和 Zend Config。
我想使用 Zend Service Manager 的功能,只要代码进入我的 Core 类,我想初始化 DB 和 Config 对象并将它们作为服务保存在 Zend Service Manager 中。现在我想在我的代码库中使用这个服务。
有没有办法在每个类的构造函数中不传递服务管理器来使用它?此外,我的其他类不会从这个 Core 类扩展,而是这个 Core 类将根据我的要求创建对象。
这就是我创建服务的方式:
$config = \Zend\Config\Factory::fromFiles([
APPLICATION_PATH . '/config/config.ini',
APPLICATION_PATH . '/config/' . $route->getMatchedParam('brand') . '/' . $route->getMatchedParam('department') . '/config.ini',
]);
$serviceManager = new ServiceManager([
'factories' => [
stdClass::class => InvokableFactory::class,
],
]);
$serviceManager->setService('config', $config);