10

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 :))?

0

6 Answers 6

11

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.

Sign up to request clarification or add additional context in comments.

Comments

10

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.

5 Comments

I think you mean "that that variable is not global"?
@Thomas No, I mean global; You cannot declare a variable that's outside of a class as protected or private, nor inside a function. So, any variable that begins with an underscore must be global - per my coding style.
@Thomas If it's public, it can be accessed from anywhere inside and outside of the class; if it's protected, from anywhere inside the current class and all inheriting classes; if it's private, only the current class.
Bit of side issue, but... the use of the word "global" here is confusing. If a variable is global then it is accessible in the global-scope, it is available in the $GLOBALS array and must be prefixed with the global keyword in order to access it within functions and class methods. None of that applies here.
@w3d Perhaps, but the question and my answer relate to OOP/class-usage (hence the public/protected/private modifiers). In the mindset of "classes", the term "global" refers to the scope of the class itself and not the entire executing script. If we were outside of a class-context then yes - your argument would be 100% valid and I would edit my answer to reflect accordingly.
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.

1 Comment

I believe you meant protected function _myProtectedFunction(){}.
1

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.

Comments

0

I know this is an old topic but just to add something, I think this is a convention that comes from the fact that getters and setters cannot have the same name as the property they get or set.

Therefore, even in languages such as C++ where private properties exist, they were often named with an underscore prefix so that the getter/setter could use the non prefixed name.

This probably led to coders in other languages that do not have private properties, such as JavaScript to use that same convention to identify "private" properties.

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.