How to create object in the constructor from the same class?

   namespace Vendor\CatalogSearch\Helper;
   use Magento\Framework\App\Helper\AbstractHelper;

   class Data extends AbstractHelper
       public function __construct(
        \Vendor\CatalogSearch\Helper\Data $myHelper,  //here circular dependency error
  • Why would you want to do this? Please explain what you want to achieve in the first place. Maybe your solution of injecting the class itself is not the right way to go.
    – 7ochem
    Commented Oct 4, 2016 at 11:21
  • Actually wanted to call function B from A thru $myHelper, anyways using the current object $this, i had used it incorrectly
    – Sushivam
    Commented Oct 5, 2016 at 5:34

2 Answers 2


You cannot do that, because of 2 reasons.

  1. It doesn't make sense.
  2. The factory that should instantiate the object will throw a "circular dependency" exception.

I'll start with the second point.
If you do it like that, when you try to instantiate your helper, the dependency injection container (DIC) will look for an instance of your class to pass it to your class constructor.
But that class instance does not exist, so DIC will try to instantiate it and you are back at the previous step.

And here is why it doesn't make sense.
You would need a dependency on your class because you need to call a method from that dependency, but you can already call methods from your class inside your class. You don't need an other instance to call methods from that.

  • Ya i got that Marius, i can use this object to call the function...its working
    – Sushivam
    Commented Oct 4, 2016 at 9:12

I'm not sure why you would need such dependency.

If you need to call a function from your helper you can just do the following anywhere in your helper:

  • Yes got, dono why i was calling like that...oops
    – Sushivam
    Commented Oct 4, 2016 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.