cancel
Showing results for 
Search instead for 
Did you mean: 

Where are class constructor parameters defined in Magento2?

Where are class constructor parameters defined in Magento2?

Hello,

 

If you see AbstractExpress class for PayPal payment method, you could see:

/**
 * @param \Magento\Framework\App\Action\Context $context
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Checkout\Model\Session $checkoutSession
 * @param \Magento\Sales\Model\OrderFactory $orderFactory
 * @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
 * @param \Magento\Framework\Session\Generic $paypalSession
 * @param \Magento\Framework\Url\Helper\Data $urlHelper
 * @param \Magento\Customer\Model\Url $customerUrl
 */
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\OrderFactory $orderFactory,
    \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
    \Magento\Framework\Session\Generic $paypalSession,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    \Magento\Customer\Model\Url $customerUrl
) {
    $this->_customerSession = $customerSession;
    $this->_checkoutSession = $checkoutSession;
    $this->_orderFactory = $orderFactory;
    $this->_checkoutFactory = $checkoutFactory;
    $this->_paypalSession = $paypalSession;
    $this->_urlHelper = $urlHelper;
    $this->_customerUrl = $customerUrl;
    parent::__construct($context);
    $parameters = ['params' => [$this->_configMethod]];
    $this->_config = $this->_objectManager->create($this->_configType, $parameters);
}

The question is: how does the system know which parameters to pass to constructor and of what type? I think they are defined somewhere, but I have looked at di.xml files of this module but I have only find this:

<type name="Magento\Paypal\Controller\Express\AbstractExpress">
    <arguments>
        <argument name="paypalSession" xsi:type="object">Magento\Paypal\Model\Session</argument>
    </arguments>
</type>

Only payPal Session appears (although of different type).

 

Can you explain me this, please?

 

Thanks

Jaime

3 REPLIES 3

Re: Where are class constructor parameters defined in Magento2?

These parameters are singletons.

Magento 2 uses the reflection to find out theirs types, and construct them on demand.

Re: Where are class constructor parameters defined in Magento2?

I don't think it is that way, because I have changed parameters for the constructor but the server showed errors concerning expected parameters. The only way constructor was accepted is by using those parameter types and in that order.

 

If Magento would use reflection, it should accept different types for objects and different parameter orders, but it was not the case.

 

Regards

Jaime

Re: Where are class constructor parameters defined in Magento2?

Magento is indeed using reflection to read parameter list. di.xml configuration can declare more specific parameter types, or override types from constructor type hinting.

 

In your case, exception could happen because you did not clear generated infrastructure classes for your class (magento may generate Proxy and Interceptor for your object).

 

Whenever you modify method signature, you should remember to clear auto-generated infrastructure classes for that class in var/generation folder.