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.
Best Answer
Magento Docs are saying
Magento 2 Proxies
So, in your case
Note \Proxy suffix for the object \Magento\Checkout\Model\Session