Back to Top

What is a circular dependency?

Updated 20 July 2023

Circular dependency is when class “A” depends on class “B” to use their property for any requirement and the same class “B” depends on class “A” for some dependency in Project. This means to compile Project A you must first compile Project B, but you can’t do that as B requires A to be compiled. This is the problem that circular dependencies cause.

Example:-

class A {
    public $b;

    public function __construct(B $b) {
        $this->b = $b;
    }
}

class B {
    public $a;

    public function __construct(A $a) {
        $this->a = $a;
    }
}

Error:

Circular dependency: Magento\Store\Model\ResourceModel\Config\Collection\Scoped depends on Magento\Store\Model\ResourceModel\Config\Collection\Scoped and vice versa.
circular dependency

How to Resolve Circular Dependency?

  1. To fix the problem. Create a third class “C,” which contains the classes that both A and B depend on. So they no longer depend on each other.
  2. Combine class “A” and class “B” so you don’t need to depend on other classes if both classes code combine.

What is dependency injection in Magento 2?

Searching for an experienced
Magento Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home