Protected properties prefixed with underscores

2019-04-25 08:51发布

问题:

Like:

public
  $foo        = null,
  $bar        = 10;

protected

  $_stuff     = null,
  $_moreStuff = 5;

A lot of people seem to do this. Why?

Isn't this inconsistent naming (like some PHP functions are :))?

回答1:

It really comes down to one thing: personal preference.

I, personally, am also one who uses that naming convention. Prefixing anything that is protected or private with an underscore, be it a variable or a function, lets myself and any other programmer who I regularly work with know that that variable is global and will not be accessible outside of the current class/context.

An example that helps clarify the use-case would be with class methods:

class Example {
    public function firstFunction() {
        // do stuff
    }

    protected function _secondFunction() {
        // do more stuff
    }
}

When I'm writing code that uses the class Example, or working inside of the class itself, if I see _secondFunction() I will immediately know that it's not a public function because of the starting _ and thereby not accessible outside of the class; no need to go and find the actual function declaration and see the modifier. On the flip side, I will know that firstFunction() is public because it doesn't begin with one.



回答2:

It's an old convention from the times before php5.

Php4 only had public visibility, and somehow people wanted to be able to tell if a property is meant to be public or private (same for methods). The underscore prefix denoted private members or methods, that were not meant to be used from the outside.

Although strictly speaking it is unneccessary with php5, where you can explicitly mark a class member's visibility, this convention is still common. Users argue that this makes skimming code easier, as you can immediately see if you can also call that function from outside or not. This is up to personal preference, or the given project's coding style.



回答3:

The underscore is symbolic to mean that the variable has some special property (which is predefined, and can change from organization to organization or programmer to programmer). Traditionally it means something is not public. Even functions are written sometimes as:

protected function _myProtectedFunction(){}

However also note that PHP reserves all function names starting with __ (two underscores) as magical and it is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.



回答4:

By way of comparison, Python doesn't have private/public by design and uses underscores to communicate the distinction. See this section of PEP 8:

http://www.python.org/dev/peps/pep-0008/#designing-for-inheritance

So it's not necessarily just an old pre-PHP5 convention--people could be doing it intentionally for the same reason Python does.



回答5:

It's most definitely just personal preference. They might do it because of $_GET and $_SESSION are already done like that in PHP.