Reason for using underscores in private / protected properties?Unit Test for overwrite collection class in magento2main.CRITICAL: Plugin class doesn't existDoes Magento 2 follow the PSR2 or Zend standard?Standard practice of Magento 2 extension developmentWhy Getting categories and names on product view page Magento 2 fails?factory injection in magento 2 not workingI have created an extension to show Customer Company Name in Order grid. But when creating new order, order is not showing in order gridMagento 2: Plugin class does not existMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.3 Can't view module's front end page output?
Can I use my laptop, which says 100-240V, in the USA?
Word for being out at night during curfew
Anatomically Correct Carnivorous Tree
What food production methods would allow a metropolis like New York to become self sufficient
How do I tell my supervisor that he is choosing poor replacements for me while I am on maternity leave?
Plastic-on-plastic lubricant that wont leave a residue?
Reaction of borax with NaOH
What's the word for the soldier salute?
As programers say: Strive to be lazy
When a land becomes a creature, is it untapped?
How can I answer high-school writing prompts without sounding weird and fake?
Why is this int array not passed as an object vararg array?
What happens if a creature that would fight isn't on the battlefield anymore?
Surely they can fit?
How to make a language evolve quickly?
Why is “Ich wusste, dass aus dir mal was wird” grammitally correct?
A cryptic tricolour
Do atomic orbitals "pulse" in time?
On what legal basis did the UK remove the 'European Union' from its passport?
Why doesn't Rocket Lab use a solid stage?
Create a list of all possible Boolean configurations of three constraints
Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?
How are one-time password generators like Google Authenticator different from having two passwords?
Front derailleur hard to move due to gear cable angle
Reason for using underscores in private / protected properties?
Unit Test for overwrite collection class in magento2main.CRITICAL: Plugin class doesn't existDoes Magento 2 follow the PSR2 or Zend standard?Standard practice of Magento 2 extension developmentWhy Getting categories and names on product view page Magento 2 fails?factory injection in magento 2 not workingI have created an extension to show Customer Company Name in Order grid. But when creating new order, order is not showing in order gridMagento 2: Plugin class does not existMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.3 Can't view module's front end page output?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom: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(
MagentoStoreModelStoreManagerInterface $_storeManager
)
$this->_storeManager = $_storeManager;
and another tutorial will have:
protected $_storeManager;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
)
parent::__construct($context);
$this->_storeManager = $storeManager;
And yet another will have
protected $storeManager;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $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."
magento2 php magento2.2
add a comment |
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(
MagentoStoreModelStoreManagerInterface $_storeManager
)
$this->_storeManager = $_storeManager;
and another tutorial will have:
protected $_storeManager;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
)
parent::__construct($context);
$this->_storeManager = $storeManager;
And yet another will have
protected $storeManager;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $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."
magento2 php magento2.2
add a comment |
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(
MagentoStoreModelStoreManagerInterface $_storeManager
)
$this->_storeManager = $_storeManager;
and another tutorial will have:
protected $_storeManager;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
)
parent::__construct($context);
$this->_storeManager = $storeManager;
And yet another will have
protected $storeManager;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $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."
magento2 php magento2.2
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(
MagentoStoreModelStoreManagerInterface $_storeManager
)
$this->_storeManager = $_storeManager;
and another tutorial will have:
protected $_storeManager;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
)
parent::__construct($context);
$this->_storeManager = $storeManager;
And yet another will have
protected $storeManager;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $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."
magento2 php magento2.2
magento2 php magento2.2
asked Sep 7 '18 at 18:58
Erik BErik B
132
132
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
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
add a comment |
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(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
VendorNameModelMyClass $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.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f241367%2freason-for-using-underscores-in-private-protected-properties%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Sep 7 '18 at 19:52
KAndyKAndy
16.3k23346
16.3k23346
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Sep 7 '18 at 21:25
PЯINCƏPЯINCƏ
8,57931147
8,57931147
add a comment |
add a comment |
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(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
VendorNameModelMyClass $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.
add a comment |
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(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
VendorNameModelMyClass $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.
add a comment |
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(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
VendorNameModelMyClass $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.
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(
MagentoFrameworkAppHelperContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
VendorNameModelMyClass $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.
answered Sep 7 '18 at 19:51
MarjanMarjan
861313
861313
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f241367%2freason-for-using-underscores-in-private-protected-properties%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown