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 :))?
The underscore _ is simply an acceptable character for variable/function names; it has no additional functionality. Underscore variable is the standard for private variables and methods. There is no true class privacy in JavaScript.
function Base(param1, _protected) { var _public = this; var _protected = _protected || {}; var _private = {}; // Declare some variables _public. shared = "Anyone can access this!"; _protected. inherited = "This is protected"; _private. secretVar = "Children cannot access this."; // Let's try a few functions.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With