What's the Deal With a Leading Underscore in PHP Class Methods

What's the deal with a leading underscore in PHP class methods?

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.

PHP function name starting with ONE underscore

The use of leading underscores is somewhat dictated by opinion, though it could be seen as a form of Hungarian notation, a "crutch" that programmers use to know what type of function it is.

Disregarding magic methods, double underscore would have been used to indicate private methods whereas a single underscore would mean a protected method.

This convention was introduced because PHP 4 OOP didn't have visibility modifiers such as protected and private. With the introduction of PHP 5, you no longer need this.

Why do I see underscore prefixed in front of functions and variables in PHP?

It is a convention that means that the method/property is private

PS: as long as it is just a convention it could be applied to protected and anything else as well

EDIT 5/2016

This is now not recommended, see this

Why we declare some variables in PHP as like $_variablename ...?

It's a naming convention.

From the pear manual on naming conventions:

Private class members are preceded by a single underscore. For example:

$_status

Why underscores(_) is used in functions like function _save_cart in php codeigniter?

In objective languages functions and variables names seperate with capitalize char, forexample: saveCart(), getCart(), ...

But in functional languages people use underscore and i think it comes from C posix standarts/notations.

C language doesn't support private and protected keywords so people generally use one or two underscore as pre extension for make the function private or protected.

Php firstly created as a functional language after a long time ZEND added object oriented features. So many people used to write it like a functional language. And so if you see a function like _save_cart you can understand that: an old school php coder make this function protected or private with posix way.

what does underline before function mean in php?

There's no significance as far as the language itself is concerned, but a single-underscore prefix is usually used to indicate "private" or "hidden" class members.

Sometimes you see the underscore prefixed to all private and protected members, just to make it more obvious that they aren't publicly available. Other times, regardless of the access level, an underscore can indicate an "internal" function, one that can technically be used, but the use of which is discouraged.

What does PHP's underscore function do?

If you open http://www.php.net/_ you get to the documentation of the _() function and see its for localization stuff (gettext).

What's the deal with a leading underscore in PHP class methods?

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.



Related Topics



Leave a reply



Submit