Magento 2 – How to Fix “Cannot Instantiate Abstract Class” Error

Introduction

If you are developing a Magento 2 extension or working with third-party modules, you may encounter the following error:

Cannot instantiate abstract class Magento\\Framework\\Model\\ResourceModel\\AbstractResource at

or

Cannot instantiate abstract class Magento\Framework\Model\ResourceModel\AbstractResource in

This error can be confusing, but it usually relates to how constructor arguments are defined in your class. In this guide, we will explain the common cause and how to fix it.

Why This Error Occurs

The “Cannot instantiate abstract class” error often happens when a class’s __construct() method has a default parameter placed incorrectly. In Magento 2, any argument with a default value should always be at the end of the constructor parameters.

For example, consider this constructor:

public function __construct(
Template\Context $context,
\PL\Snippets\Model\VideoFactory $videoFactory,
\Magento\Framework\Registry $coreRegistry,
array $data = [],
\PL\Snippets\Helper\Data $snippetHelper
) {
parent::__construct($context, $data);
$this->videoFactory = $videoFactory;
$this->_coreRegistry = $coreRegistry;
$this->snippetHelper = $snippetHelper;
}


Here, array $data = [] is not in the last position, which can trigger the error when Magento tries to instantiate the class.

How to Fix the Error

To resolve the issue, move any constructor parameter with a default value to the end:

public function __construct(
Template\Context $context,
\PL\Snippets\Model\VideoFactory $videoFactory,
\Magento\Framework\Registry $coreRegistry,
\PL\Snippets\Helper\Data $snippetHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->videoFactory = $videoFactory;
$this->_coreRegistry = $coreRegistry;
$this->snippetHelper = $snippetHelper;
}

 After this change, Magento 2 can correctly instantiate your class, and the error will be resolved.