About PHP underscore naming convention (as in "_method" or "_property")
Asked Answered
C

5

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?

Church answered 23/4, 2011 at 18:40 Comment(0)
F
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.

Forty answered 23/4, 2011 at 18:41 Comment(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.Professorship
@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 wellForty
I see, thanks! That's a useful insight. I'll stick to this convention, I find it practical and natural.Church
B
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

Bonilla answered 29/9, 2013 at 20:12 Comment(2)
What's the reasoning behind this convention? I couldn't see any at a glance.Monongahela
@Monongahela 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).Bonilla
A
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/

Afc answered 4/7, 2017 at 2:11 Comment(0)
H
4

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.

Handbarrow answered 18/7, 2021 at 14:15 Comment(0)
B
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 )

Basis answered 11/4, 2015 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.