varan
varan

Reputation: 354

DI in Zend Framework 2. How to make cyclic dependancy?

How to implement this:

<?php
class MyClass1
{
     public function __construct(MyClass2 $objClass2)
     {
          $this->objClass2 = $objClass2;
      }
}

class MyClass2
{
     public function setClass1(MyClass1 $objClass1)
    {
          $this->objClass1 = $objClass1;
     }
}
?>

I mean I need to create a MyClass2 object, put it into MyClass1 constructor and then I need MyClass1 object to be put into MyClass2 object that was created before via setClass1 method. Is it possible to do in DI config in ZendFramework?

Upvotes: 3

Views: 257

Answers (1)

dbrumann
dbrumann

Reputation: 17166

You don't want this! If you injected Class2 into Class1 you already have access to it, by using $class1->class2->somethingSomething();, or you can retrieve it by $class->getClass2(); and so on... I don't know what your actual scenario is, but when you have cyclic dependecy, something's gone awry.

Upvotes: 3

Related Questions