37

Playing around with Magento 2 but run into a problem I can not wrap my head around. The object manager tries to instantiate Abstract class, this gives an Fatal Error. Does someone know what might cause this or point me in the right direction?

The Error

Fatal error: Cannot instantiate abstract class Magento\Framework\Model\Resource\AbstractResource in /lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Developer.php on line 75

The Problem

My module has a model that extends the \Magento\Quote\Model\Quote. The constructor of this model has the following line injecting this:

\Magento\Framework\Model\Resource\AbstractResource $resource = null,.

The controller that is instantiating the model extends the \Magento\Backend\App\Action and uses $this->_objectManager->create() to instantiate the model.

The thing I can not wrap my head around is if I instantiate the Magento model it just works but if I instantiate my model it tries to instantiate this abstract class throwing the error above. What tells the object manager to do this and how can I solve my problem?

My first thought was Magento uses the di.xml to replace this, execute this or something, this was not the case.

Thanks for taking the time to read this. Any help or input on this issue is appreciated.

6
  • can you post your code in the question?
    – Marius
    Aug 4 '15 at 13:57
  • Could you perhaps be a bit more specific? The whole controller class and instantiated model are huge and adding them to the question does not make it easier to comprehend the initial problem. Will the action function or the constructor suffice?
    – rofavadeka
    Aug 4 '15 at 14:15
  • Fair enough. Post your class name, what it extends, what it implements and the __construct method of your model.
    – Marius
    Aug 4 '15 at 14:19
  • As requested :) the top part of the model. I think Chris has the solution, going to test this and get back to you. Thanks Marius.
    – rofavadeka
    Aug 4 '15 at 14:54
  • Yep. I think Chris got it. :)
    – Marius
    Aug 4 '15 at 15:03
112

This can happen if your sub-class added new dependencies after the existing optional dependencies of the parent class.

Snippet from the parent

    \Magento\Quote\Model\Cart\CurrencyFactory $currencyFactory,  // required
    JoinProcessorInterface $extensionAttributesJoinProcessor,    // required
    \Magento\Framework\Model\Resource\AbstractResource $resource = null,       //optional
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,  //optional
    array $data = [] //optional
)}

How code might alter what is optional

    \Magento\Quote\Model\Cart\CurrencyFactory $currencyFactory,  // required
    JoinProcessorInterface $extensionAttributesJoinProcessor,    // required
    \Magento\Framework\Model\Resource\AbstractResource $resource = null,       // required (even though it has a default value, because it is followed by parameters that don't have default values)
    int $x, // required
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,  // optional (still optional because there are no required parameters following it)
    array $data = [] // optional
)}

The Magento 2 Object Manager will try to inject any required parameters. So this could happen if you added a required parameter at the end of the constructor.

You can fix this by moving any new required parameter up above the optional ones.

2
  • My issue was that when using auto-complete in phpStorm it failed to also bring with the = null part :/
    – OZZIE
    Sep 26 '17 at 12:37
  • surprising i have same issue in one of the server but same code is working fine in other server. not sure what is the issue ? why same code is working one of the server not in other Jun 23 '20 at 11:46
0

Sometimes some module was updated and the references under /generated were not updated yet.

Just do a bin/magento setup:di:compile and try again.

(Worked for me when updating a payment module and /checkout didn't load)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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