22

This is a sort of general inquiry I've been wondering about. I've noticed a lot of this through other people's code, and never really knew the actual reason, just followed the trends, so here goes.

How come some methods and properties are named with an underscore in front, and others aren't? For example, when specifically would one use function _method(), and when would one use function method(), or, in other words, private $_someVariable vs. private $someVariable?

j0k
  • 22,600
  • 28
  • 79
  • 90
Swader
  • 11,387
  • 14
  • 50
  • 84

5 Answers5

26

Most of the time, it's a throwback convention to PHP4 which didn't support visibility for properties or methods, and library developers used the _ to indicate something that should be considered private, and not to be accessed directly from outside of the class. PHP5 does have visibility, but the convention is still often maintained.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 3
    Never realized that was the reasoning behind it, thanks for sharing that. I do find it helpful and still use the convention however, it lets the private methods "share" names with public ones, and makes them stick out a bit. – Wesley Murch Apr 23 '11 at 18:48
  • 1
    @Madmartigan - It certainly means that a call to a private method or access to a private property from within the class is instantly recognisable as well – Mark Baker Apr 23 '11 at 19:48
  • I see, thanks! That's a useful insight. I'll stick to this convention, I find it practical and natural. – Swader Apr 24 '11 at 02:42
14

Now, in 2013, this is "officially" bad style by the PSR-2 coding guideline:

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility`

Source: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

Sliq
  • 15,937
  • 27
  • 110
  • 143
  • What's the reasoning behind this convention? I couldn't see any at a glance. – w5m Dec 04 '13 at 14:05
  • 2
    @w5m I don't know, but you could ask the PSR-guys, they usually answer question very fast and public (they have a "google group" somewhere). – Sliq Dec 04 '13 at 14:51
9

***Follow the PSR-2 coding guideline:

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility`

Source: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

***Reason :

No underscores before the property name, like $_income, instead use $income. The underscore was used in some frameworks and can be confused with PHP magic variables.

Source : http://www.geekgumbo.com/2013/05/19/psr-2-coding-style-guide/

2

Just in case, new PSR-12 say it MUST NOT have underscores:

https://www.php-fig.org/psr/psr-12/

4.3 Properties and Constants Property names MUST NOT be prefixed with a single underscore to indicate protected or private visibility.

4.4 Methods and Functions Method names MUST NOT be prefixed with a single underscore to indicate protected or private visibility.

Luciano Fantuzzi
  • 916
  • 12
  • 23
1

This is offical document from php.net say nothing about underscore stand before private methods, private fields.

But follow Zend Framework coding convention:

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.

Therefore, we should start naming a private method with an underscore :)

Notice:

PHP reserves all symbols starting with __ as magical. It is recommended that you do not create symbols starting with __ in PHP unless you want to use documented magical functionality.

( Source: http://php.net/manual/en/userlandnaming.rules.php )

Vy Do
  • 46,709
  • 59
  • 215
  • 313