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.

  • 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

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.

| improve this answer | |
  • 1
    Chris, You have earned a massive Thank-You! You solved my problem big time. – rofavadeka Aug 4 '15 at 15:02
  • 1
    And a second BIG THANK YOU. I love you bro – Yonn Trimoreau Mar 2 '16 at 20:41
  • @Chris solution provided by you helped me, thanks – Mukesh Jun 13 '17 at 10:33
  • Great explaination! – Henry Bui Jul 14 '17 at 4:47
  • 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

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)

| improve this answer | |

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.