Magento 2.4.6 – Deprecated Functionality: Creation of Dynamic Property $_coreRegistry

deprecatedexceptionmagento2.4.6php8php8.2

I am having this issue with magento 2.4.6 custom module and php8.2

This code:

public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
    ) {
        $this->messageManager = $messageManager;
        $this->_storeManager = $storeManager;
        $this->_objectManager = $objectManager;
        $this->_coreRegistry = $registry;
        $this->_localeDate = $localeDate;
        parent::__construct($context);
    }

line with

$this->_coreRegistry = $registry;

Throw error:

Exception #0 (Exception): Deprecated Functionality: Creation of dynamic property \Helper\Data::$_coreRegistry is deprecated in app/code/MyFolder/Helper/Data.php on line 47

I have no clue what to change and fix this

Any help please?

Best Answer

Deprecated Functionality: Creation of dynamic property \Helper\Data::$_coreRegistry is deprecated

You must declare this property ($_coreRegistry) in your class.
To do this, add the following code above the __construct method:

/**
 * @var \Magento\Framework\Registry
 */
private $_coreRegistry;

There is another solution is changing this property ($_coreRegistry) in your class to match the $argument in the __construct method, which is $registry. To do this, replace all text _coreRegistry with registry.

Here, I highly recommend using the second solution as it leverages the constructor property promotion (new feature) in PHP 8. To view a demonstration of this feature, you can watch the following video: https://www.youtube.com/watch?v=I960JDkmXY0

Related Topic