2

I just started working in Magento 2 , What is "_construct" mandate for

My question are
1. When they are used (as in some files they are not there) ?

<?php
namespace Mycompany\Customerhomepage\Block;
use  Magento\Framework\View\Element\Template\Context;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;



class Landing extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Mycompany\Customerhomepage\Model\landing
     */
    protected $_normCollectionFactory;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;
    protected $_categoryRepository;
    protected $_urlInterface;
    protected $_storeManager;
    protected $_tableFactory;
    protected $_attributeRepository;
    protected $_attributeValues;


    /**
     * @param Context $context
     * @param StoreManagerInterface $storeManager
     * @param \Mycompany\Customerhomepage\Model\landing $_normCollectionFactory
     * @param \Magento\Customer\Model\Session $_customerSession
     * @param \Magento\Catalog\Model\CategoryRepository $_categoryRepository
     * @param \Magento\Eav\Model\Entity\Attribute\Source\TableFactory $_tableFactory
     * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $_attributeRepository
     * @param array $data

     */
    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        \Mycompany\Customerhomepage\Model\landing $_normCollectionFactory,
        \Magento\Customer\Model\Session $_customerSession,
        \Magento\Catalog\Model\CategoryRepository $_categoryRepository,
        \Magento\Eav\Model\Entity\Attribute\Source\TableFactory $_tableFactory,
        \Magento\Catalog\Api\ProductAttributeRepositoryInterface $_attributeRepository,
        array $data = []

    )
    {
        $this->_normCollectionFactory = $_normCollectionFactory;
        $this->_customerSession = $_customerSession;
        $this->_categoryFactory = $_categoryRepository;
        $this->_storeManager = $storeManager;
        $this->_tableFactory = $_tableFactory;
        $this->_attributeRepository = $_attributeRepository;

        parent::__construct($context);
    }
  1. What is significance of \Magento\Eav\Model\Entity\Attribute\Source\TableFactory $_tableFactory in above code
  2. what is significance of array $data = []
  3. Why this is done 'this->_tableFactory = $_tableFactory;'
  4. what is use for this parent::__construct($context); , when is had to extend product_list class i had to pass all the same variable that was in product_list to my class constructor like this parent::__construct($context,$postDataHelper,$layerResolver,$categoryRepository,$urlHelper); , why that so ?

I would be happy even if direct me to some articles to read , as i was not sure what to search for (I thought it was DI but i guess it was just half of it )

1

When they are used (as in some files they are not there) ?

I assume you're referring to the _construct methods here. Those methods are constructors. They're usually present when the class needs to use other classes which are injected via Dependency Injection. I suggest you have a read at this great article that explains everything about DI: http://alanstorm.com/magento2_dependency_injection_2015

What is significance of \Magento\Eav\Model\Entity\Attribute\Source\TableFactory $_tableFactory in above code

As explained above, this class is needed in the code to work properly. The $_tableFactory parameter is assigned to a protected variable $this->_tableFactory in order to be used once the class has been instantiated.

What is significance of array $data = []

As this question comes up very often I suggest you check the related post about it here: Magento 2: what is the $data array constructor parameter?

Why this is done 'this->_tableFactory = $_tableFactory;'

As said in #2 if we don't assign the parameter to a class variable we won't be able to use that class to do what we need, that's why such code exists in every constructor of Magento 2.

What is use for this parent::__construct($context); , when is had to extend product_list class i had to pass all the same variable that was in product_list to my class constructor like this parent::__construct($context,$postDataHelper,$layerResolver,$categoryRepository,$urlHelper); , why that so ?

Well it's a case by case answer but when you use preference to extend an existing class of Magento 2 you have to respect the original constructor of the class you extend. Thus if you need to use more classes you will have to modify the constructor by injecting your extra classes, assign them to variables and then call the parent constructor with the original arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.