magento2helpermage

What would the magento 2 equivalent of Mage::helper('core')->?


What would the magento 2 equivalent of Mage::helper('core')-> ?


Solution

  • The Mage static methods are not existing anymore, you will have to use dependency injection to get your helper instance, for example in your model:

    <?php
    namespace Mycompany\Mymodule\Model;
    use Mycompany\Mymodule\Helper\Data;
    class Custom {
    
        private $helper;
    
        public function __construct(
            Data $helper
        ) {
            $this->helper = $helper;
        }
    
        public function myMethod() {
            $this->helper->helperMethod();
        }
    }
    

    You can use the same system in Blocks etc.. and for existing helpers that you want to use.