views:

3848

answers:

7

While looking over various PHP libraries I've noticed that a lot of people choose to prefix some class methods with a single underscore, such as

public function _foo()

...instead of...

public function foo()

I realize that ultimately this comes down to personal preference, but I was wondering if anyone had some insight into where this habit comes from.

My thought is that it's probably being carried over from PHP 4, before class methods could be marked as protected or private, as a way of implying "do not call this method from outside the class". However, it also occurred to me that maybe it originates somewhere (a language) I'm not familiar with or that there may be good reasoning behind it that I would benefit from knowing.

Any thoughts, insights and/or opinions would be appreciated.

+7  A: 

Leading underscores are generally used for private properties and methods. Not a technique that I usually employ, but does remain popular among some programmers.

jonstjohn
+1  A: 

I believe your original assumption was correct, I have found it to be common practice for some languages to prefix an underscore to methods/members etc that are meant to be kept private to the "object". Just a visual way to say although you can, you shouldn't be calling this!

Quintin Robinson
+10  A: 

It's from the bad old days of Object Oriented PHP (PHP 4). That implementation of OO was pretty bad, and didn't include things like private methods. To compensate, PHP developers prefaced methods that were intended to be private with an underscore. In some older classes you'll see /**private*/ __foo() { to give it some extra weight.

I've never heard of developers prefacing all their methods with underscores, so I can't begin to explain what causes that.

Jeremy DeGroot
I place an underscore before methods in my controllers that are private to the class, and unused in routing. Because I work with my own framework, this adds security as I enforce the policy of no leading underscore on controller names within routes. But this rarely exceeds 1-2 methods per controller.
The Wicked Flea
By convention, with perl, method beginning with an underscore are private. But it's only a convention. In fact, these methods are still accessible from outside the class.
Luc M
+1  A: 

I use a leading underscore in the PHP 5 class I write for private methods. It's a small visual cue to the developer that a particular class member is private. This type of hinting isn't as useful when using an IDE that distinguishes public and private members for you. I picked it up from my C# days. Old habits...

GloryFish
+5  A: 

I believe the most authoritative source for these kinds of conventions for PHP right now would be the Zend Framework coding convention: http://framework.zend.com/manual/en/coding-standard.naming-conventions.html#coding-standard.naming-conventions.functions-and-methods

For methods on objects that are declared with the "private" or "protected" modifier, the first character of the method name must be an underscore. This is the only acceptable application of an underscore in a method name. Methods declared "public" should never contain an underscore.

SNIP

For instance variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared "public" should never start with an underscore.

joedevon
Gotta love the PHP kids and their undyingLoveForJava.
jrockway
Using a naming convention is not a reason to love a language, I think.
Sepehr Lajevardi
joedevon
A: 

I know it from python, where prefixing your variables with an underscore causes the compiler to translate some random sequence of letters and numbers in front of the actual variable name. This means that any attempt to access the variable from outside the class would result in a "variable undefined" error.

I don't know if this is still the convention to use in python, though

Matthew
A: 

They are called "magic methods".

Tristan
`_foo()` with a single leading underscore is not a magic method. Magic methods are denoted by *two* consecutive leading underscores. The question here talks about only one.
BoltClock