Answer the question
In order to leave comments, you need to log in
How to solve the problem of "Circular dependency" in Zend 2 or even competently organize dependency injection?
There are 2 services ( TableGateway ) and factories for them: Day and DaySet .
public function __invoke(ServiceLocatorInterface $serviceLocator) {
// ...
$daySetTable = $serviceLocator->get(DaySetTable::class);
return DayTable(..., $daySetTable);
}
$daySetId = $item->getDayset()
/** @var DaySet $daySetObject */
$daySetObject = $this->daySetTable->get($daySetId);
$item->setDayset($daySetObject);
Answer the question
In order to leave comments, you need to log in
Put a limit on the number of cycles.
In the class, write a certain variable that will decrease to a certain value with each iteration.
The problem of circular dependency is solved by initializing one of the dependencies on the first call, and not when instantiating.
class a {}
class b {
public function _construct($locator){}
public function someOperation() {
$this->dependency = $this->dependency ?? $this->locator->get(a::class);
//.....
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question