0

Often I'll be going through tutorials written by various people, and see underscores used for private or protected vars/properties within a class.

e.g., one person will will have:

protected $_storeManager;

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $_storeManager
)
{
    $this->_storeManager = $_storeManager;
}

and another tutorial will have:

protected $_storeManager;

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ) {
   parent::__construct($context);
    $this->_storeManager = $storeManager;
}

And yet another will have

protected $storeManager;

public function __construct(
  \Magento\Framework\App\Action\Context $context,
  \Magento\Store\Model\StoreManagerInterface $storeManager,
) {
  parent::__construct($context);
  $this->storeManager = $storeManager;
}

Is there any rhyme or reason for this? Is there a reason you would ever use a _ for a protected or private class property? Magento docs say they follow PSR-2 standards, which states

"Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility."

1

Magento coding convention follows PSR2 and do not recommend starting protected properties from underscore.

Some properties still start from underscore only in legacy code or for Backward compatibility reason

1

Magento 2 :

The Magento core development team uses the PSR-1: Basic Coding Standard and PSR-2: Coding Style Guide. Magento recommends that developers who create Magento extensions and customizations also use these standards.

Source : Magento DevDocs PHP coding standard


PSR-2 :

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.

Source : PSR-2 Coding Style Guide

0

Magento certainly tries to follow standards, but often does not quite follow through.

Underscoring protected properties, in my opinion, is Magento's attempt to somehow set a coding convention for itself.

This being their signature would open an opportunity to easy distinguish the "native" properties from the ones developed by the community, if (in theory) the coding would be set like

protected $_storeManager;

protected $objectFromACustomModule;

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Vendor\Name\Model\MyClass $myObject
) {
    parent::__construct($context);
    $this->_storeManager = $storeManager;
    $this->objectFromACustomModule = $myObject;
}

Just for comparison, I went into quick browse through the /vendor folder of a Symfony 4 application, and from what I've seen from a random set of a two dozen of classes, none of the classes uses underscore in front of the property name. Just regular old camel casing.

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.