Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the convention of __ means in php?

I saw some ppl write code like this:

They use __ in the naming of a variable:

   public static function getInstance(){
        if(self::$__instance == NULL) self::$__instance = new SCFormatter();
        return self::$__instance;
    }

not only variable, but also the function:

   private function __clone(){}

Is there any special meaning for php coder to use "__" as a prefix. Thank you.

like image 455
Tattat Avatar asked Jul 25 '11 05:07

Tattat


People also ask

What starts with __ in PHP?

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. All methods names starting with __ are reserved by PHP.

What are the naming conventions in PHP?

Function names use underscores between words, while class names use both the camelCase and PascalCase rules. PHP will prefix any global symbols of an extension with the name of the extension. (In the past, there have been numerous exceptions to this rule.)

What starts with __ double underscore in PHP?

Whenever you see a function name start with a double-underscore, it is a "magic" function - one that PHP has provided that you have not declared yourself.

What does the underscore do in PHP?

In PHP, the underscore is generally reserved as an alias of gettext() (for translating strings), so instead it uses a double-underscore. All the functions that make up the library are available as static methods of a class called __ – i.e., a double-underscore. …and so on.


2 Answers

__ in PHP is for magic methods like __get, __set, __clone

Historically _ before variable or function means it's private since there was no private, public or protected methods in PHP 4.

It is applied not only to PHP. In Python for example, _ prefix for functions and variables is used for the same purpose.

I suggest to avoid naming your functions prefixing them with __ (double underscore) since PHP developers can add the same magic function in next versions of the language, so it would break your code. You can use one underscore still - it is not dangerous, since it can't affect the language features. Although it's possible, please notice that using _ for property / method name prefix is not advised since it doesn't comply with most of the coding standards nowadays.

like image 172
Nemoden Avatar answered Oct 03 '22 21:10

Nemoden


There's no special syntactic meaning. However, it's highly discouraged by the PHP team to use __ at the beginning of names, because you might break future core PHP features.

PHP tries to prefix core names with __ so they don't interfere with your names. For example, there's a __construct() method, a __sleep() method, a __wakeup() method, etc. They didn't call them construct(), sleep() and wakeup() because they might interfere with your own names.

This is a general practice. Same in the Linux kernel for example. System functions are prefixed with __ so they don't interfere with your naming. If you start prefixing your own names with __, then you'll start interfering with their naming, which will either break system functions or most likely your application in the future, when you least expect it.

If the PHP team somehow decides to create a static variable in each class with the name $__instance, then all your scripts will start breaking suddenly and you won't know why.

Quoting the caution in the PHP manual page for Magic Methods:

PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

And about __clone() — it's a PHP feature, going farther to demonstrate that prefixing your own names with __ can be pretty confusing as well.

like image 42
rid Avatar answered Oct 03 '22 21:10

rid