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.
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.
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.)
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.
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.
__
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With