Magento 2 classes have dependency injection in constructor method. One special object which is there is context.
The context object further provides many other objects. Apparently, if you have injected context object in your module's class which may further inheriting some Magento 2 class, you may not need to inject some objects which are already being provided by context object. This mechanism makes sure, your system does not end up with many duplicate objects occupying memory and leads to poor performance.
When your Magento 2 shop runs with developer mode, the system does not report any issue. But when you switch the mode to production, it reports the error.
Also, when you run php bin/magento setup:di:compile command, the system reports the error.
Now, to the point, how to solve the error which looks like something below, the class name may be different.
Errors during compilation:
Helper\Data
Incorrect dependency in class Helper\Data in vendor/xxx/Helper/Data.php
\Magento\Framework\SOME\CLASS already exists in context object
Total Errors Count: x.
As we have learned above, the object is already being provided by the context object. So, it does not need to be injected, instead retrieved from context object.
Step 1: Remove the object from your constructor which was reported as already exists.
Step 2: Assign your class property by retrieving the object from context object.
For example, if your constructor method looks like below and reported error for class ScopeConfigInterface.
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->_scopeConfig = $scopeConfig;
parent::__construct($context);
}
The rectification will look like below.
public function __construct(
\Magento\Framework\App\Helper\Context $context
) {
$this->_scopeConfig = $context->getScopeConfig();
parent::__construct($context);
}
The context object further provides many other objects. Apparently, if you have injected context object in your module's class which may further inheriting some Magento 2 class, you may not need to inject some objects which are already being provided by context object. This mechanism makes sure, your system does not end up with many duplicate objects occupying memory and leads to poor performance.
When your Magento 2 shop runs with developer mode, the system does not report any issue. But when you switch the mode to production, it reports the error.
Also, when you run php bin/magento setup:di:compile command, the system reports the error.
Now, to the point, how to solve the error which looks like something below, the class name may be different.
Errors during compilation:
Helper\Data
Incorrect dependency in class Helper\Data in vendor/xxx/Helper/Data.php
\Magento\Framework\SOME\CLASS already exists in context object
Total Errors Count: x.
As we have learned above, the object is already being provided by the context object. So, it does not need to be injected, instead retrieved from context object.
Step 1: Remove the object from your constructor which was reported as already exists.
Step 2: Assign your class property by retrieving the object from context object.
For example, if your constructor method looks like below and reported error for class ScopeConfigInterface.
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->_scopeConfig = $scopeConfig;
parent::__construct($context);
}
The rectification will look like below.
public function __construct(
\Magento\Framework\App\Helper\Context $context
) {
$this->_scopeConfig = $context->getScopeConfig();
parent::__construct($context);
}
Hope, this will help you to rectify the error appeared while running php bin/magento setup:di:compile command.
Errors during compilation:
Incorrect dependency in class x.php
some class already exists in context object
Total Errors Count: x
Incorrect dependency in class x.php
some class already exists in context object
Total Errors Count: x
Visit for affordable Magento 2 extensions which provides the most sought features only.
No comments:
Post a Comment