例えば見出されたIは、コントローラクラスをUserController
循環依存はZend Frameworkの2、</p> <p>で `UserService`
UserController
を有するUserService
UserService
に依存するUserChangedListener
に依存しています
UserChangedListener
は、SomeOtherClass
SomeOtherClass
私UserController
とSomeOtherClass
がUserService
に依存しているので、ここでUserService
に依存しています。
私は取得していますエラー:
LazyServiceLoaderため循環依存関係は、インスタンス UserService
上記のエラーが見つかりました
(すなわちLazyServiceLoader用循環依存)私はUserChangedListener
でSomeOtherClass
を注射した場合に発生と私は持っています
"zendframework/zend-servicemanager": "^ 2.7.5 ||^3.0.3" 、
UserControllerFactory.php
class UserControllerFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $container = $container->getServiceLocator(); return new UserController( $container->get(UserService::class) ); } public function createService(ServiceLocatorInterface $serviceLocator) { return $this($serviceLocator, UserController::class); } }
UserServiceFactory.php
class UserServiceFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $service = new UserService( $container->get(UserRepository::class) ); $eventManager = $service->getEventManager(); $eventManager->attach($container->get(UserChangedListener::class)); return $service; } public function createService(ServiceLocatorInterface $serviceLocator) { return $this($serviceLocator, UserService::class); } }
UserChangedListenerFactory.php
あなたはUserService
と合法的な循環依存関係を持っているように10SomeOtherClassFactory.php
class SomeOtherClassFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { $rootLocator = $serviceLocator->getServiceLocator(); return new SomeOtherClass( $rootLocator->get(UserService::class) ); } }