I'm using Ubuntu 18.04 with PHP 7.3.14. I was in my root directory and entered the command bin/magento setup:install and got this error. What is going on?
PHP Fatal error: Uncaught Error: Cannot instantiate interface Project\Crawler\Api\CrawlerItemRepositoryInterface in /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:50
Stack trace:
#0 /var/www/html/root/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Project\\Crawler...')
#1 /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(160): Magento\Framework\ObjectManager\ObjectManager->get('Project\\Crawler...')
#2 /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(246): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Project\\Crawler...', NULL, 'crawlerItemRepo...', 'Project\\ImageCr...')
#3 /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime('Project\\ImageCr...', Array, A in /var/www/html/root/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 50
I have been looking at these but they didn't work for me:
- Magento2: Fatal error: Uncaught Error: Cannot instantiate interface
- https://community.magento.com/t5/Magento-2-x-Version-Upgrades/upgrade-to-2-3-2-error-developer-php/td-p/136666
- https://community.magento.com/t5/Magento-2-x-Version-Upgrades/upgrade-to-2-3-2-error-developer-php/td-p/136666
- Cannot instantiate interface Magento\\Catalog\\Model\\Layer\\FilterableAttributeListInterface
3 Answers 3
Magento in attempting to instantiate the interface, interfaces cannot be instantiated. You need a class that implements the interface.
To do this use a preference in the di.xml that suggests which class to use for the CrawlerItemRepositoryInterface
-
Ow yea you are right. Nvm my answerCompactCode– CompactCode2020年02月10日 16:21:27 +00:00Commented Feb 10, 2020 at 16:21
Well it does not find that interface or can not load it and that can have multiple reasons.
Here a couple :
- Your interface does not exist
- The interface has the wrong namespace
- The interface is in the wrong directory structure
- The interface wrong syntaxes
- The interface is opened with a
<?tag instead of a<?phptag and your php settings does not allow shorthand php opening tags
EDIT
See Netstorm's answer. Will leave this here for debugging purposes for other people
ADDITIONALLY to a correct di.xml
In Magento 2.4.5-p1.
I found that dependency between files with the SAME $this->variable name CAN cause this issue.
Solution: Change the variable name from
$this->mymodelFactory = $mymodelFactory;
To
$this->_mymodelFactory = $_mymodelFactory;
.
.
.
Then:
Error > Cannot instantiate interface
Vendor/Module/Model/Order.php
protected MymodelFactory $mymodelFactory;
_ _construct(
...
MymodelRepositoryInterface $mymodelRepository,
MymodelFactory $mymodelFactory
){
$this->mymodelRepository = $mymodelRepository;
$this->mymodelFactory = $mymodelFactory;
}
di.xml
<preference for="Vendor\Module\Api\MymodelRepositoryInterface" type="Vendor\Module\Model\MymodelRepository"/>
Vendor/Module/Model/MymodelRepository.php
_ _construct(
...
MymodelFactory $mymodelFactory
){
$this->mymodelFactory = $mymodelFactory
}
.
.
.
NOW:
No errors
Vendor/Module/Model/Order.php
protected MymodelFactory $_mymodelFactory;
_ _construct(
...
MymodelRepositoryInterface $mymodelRepository,
MymodelFactory $_mymodelFactory
){
$this->mymodelRepository = $mymodelRepository;
$this->_mymodelFactory = $_mymodelFactory;
}
di.xml
<preference for="Vendor\Module\Api\MymodelRepositoryInterface" type="Vendor\Module\Model\MymodelRepository"/>
Vendor/Module/Model/MymodelRepository.php
_ _construct(
...
MymodelFactory $mymodelFactory
){
$this->mymodelFactory = $mymodelFactory
}