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;








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."











share|improve this question




























    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."











    share|improve this question
























      0












      0








      0


      1






      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."











      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 7 '18 at 18:58









      Erik BErik B

      132




      132




















          3 Answers
          3






          active

          oldest

          votes


















          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






          share|improve this answer






























            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






            share|improve this answer






























              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(
              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.






              share|improve this answer























                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
                );



                );













                draft saved

                draft discarded


















                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









                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






                share|improve this answer



























                  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






                  share|improve this answer

























                    1












                    1








                    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






                    share|improve this answer













                    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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 7 '18 at 19:52









                    KAndyKAndy

                    16.3k23346




                    16.3k23346























                        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






                        share|improve this answer



























                          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






                          share|improve this answer

























                            1












                            1








                            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






                            share|improve this answer













                            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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 7 '18 at 21:25









                            PЯINCƏPЯINCƏ

                            8,57931147




                            8,57931147





















                                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(
                                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.






                                share|improve this answer



























                                  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(
                                  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.






                                  share|improve this answer

























                                    0












                                    0








                                    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(
                                    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.






                                    share|improve this answer













                                    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.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 7 '18 at 19:51









                                    MarjanMarjan

                                    861313




                                    861313



























                                        draft saved

                                        draft discarded
















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                                        Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                        Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form