开发者

What is the convention of __ means in php?

开发者 https://www.devze.com 2023-03-23 03:11 出处:网络
I saw some p开发者_StackOverflow社区pl write code like this: They use __ in the naming of a variable:

I saw some p开发者_StackOverflow社区pl 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.


__ 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.


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.


All the PHP Magic methods begin with a double underscore. This includes __clone(). Although it's not enforced by PHP, naming your own methods starting with a double underscore is discouraged as these names may collide with future PHP magic methods.


They are typically there to symbolize private variables. Basically variables and methods can't have the same name so its there to provide private variables with no conflicting names. I would just use a single underscore if you going that route.

Also as mentioned other places php has magic methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号