8

I'm writing a payment module for Magento 2 right now and I'm checking it against the
"Magento Extension Quality Program Coding Standard" CodeSniffer
(https://github.com/magento/marketplace-eqp).

For every of my classes that are using a (checkout) session object, the CodeSniffer responds with the following warning:

Session object MUST NOT be requested in constructor. It can only be passed as a method argument.

I'm getting the session object in the following way:

/**
 * Checkout session object
 *
 * @var \Magento\Checkout\Model\Session
 */
protected $checkoutSession;

/**
 * Constructor
 *
 * @param  \Magento\Checkout\Model\Session $checkoutSession
 * @return void
 */
public function __construct(\Magento\Checkout\Model\Session $checkoutSession)
{
    $this->checkoutSession = $checkoutSession;
}

Is there a correct way to obtain the session object?
I couldn't find anything in the Magento 2 core code.
I could only find code where it is used in exactly the same way I use it.

  • U have missed protected $checkoutSession; before Constructor – Ankit Shah Oct 21 '16 at 1:48
  • It's there, I just didn't show it in this example-code. I added it to the example for better understandability – Robert M. Oct 21 '16 at 11:52
  • same issue here, share it if any one have solution – Nikhil Vaghela Jul 26 '17 at 10:30
4

Magento Docs are saying

If a class’s constructor is particularly resource-intensive, this can lead to unnecessary performance impact when another class depends on it, if the expensive object does not end up being needed during a particular request.

Magento has a solution for this situation: proxies. Proxies extend other classes to become lazy-loaded versions of them. That is, a real instance of the class a proxy extends is created only after one of the class’s methods is actually called. A proxy implements the same interface as the original class and so can be used as a dependency anywhere the original class can. Unlike its parent, a proxy has only once dependency: the object manager.

Proxies are generated code and therefore do not need to be manually written. (See Code generation for more information.) Simply reference a class in the form \Original\Class\Name\Proxy, and the class is generated if it does not exist.

Magento 2 Proxies

So, in your case

/**
 * Checkout session object
 *
 * @var \Magento\Checkout\Model\Session\Proxy
 */
protected $checkoutSession;

/**
 * Constructor
 *
 * @param  \Magento\Checkout\Model\Session\Proxy $checkoutSession
 * @return void
 */
public function __construct(\Magento\Checkout\Model\Session\Proxy $checkoutSession)
{
    $this->checkoutSession = $checkoutSession;
}

Note \Proxy suffix for the object \Magento\Checkout\Model\Session

2

As the warning indicates:

Session object MUST NOT be requested in the constructor

You should use proxies instead.

You can find out more about proxy objects here: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/proxies.html

0

As per Magento 2 ECGM2 coding standard you first use session class then you can pass it into constuctor otherwise this error will be shown

Session object MUST NOT be requested in constructor. It can only be passed as a method argument.

Example:

namespace vendor\module\..;

use Magento\Checkout\Model\Session as CheckoutSession;

class ClassName {
    ...

    protected $_checkoutSession;

    public function __construct(
        ....
        CheckoutSession $checkoutSession,
        ....
    ){
        ....
        $this->_checkoutSession = $checkoutSession;
        ....
    }
}
  • @Price Patel I still get the above error on using this code. Any solution?This is my code : namespace ...; use Magento\Checkout\Model\Session as CheckoutSession; use Magento\Customer\Model\Session as CustomerSession; class Test { private $checkoutSession; private $customerSession; public function __construct( CheckoutSession $checkoutSession, CustomerSession $customerSession ) { $this->checkoutSession = $checkoutSession; $this->customerSession = $customerSession; } – Vindhuja Jul 24 '18 at 7:24

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.