php _, __ 의미

2020. 1. 15. 11:15PHP/CodeIgniter3

728x90

https://forum.arduino.cc/index.php?topic=48820.0
Used in front of member variables in a class, to distinguish them (for the reader) from local variables. The presence or absence of the _ (or __) makes no difference to the compiler.

 

Used in front of variables that are part of a class.  
Not global, not local in a function, but in a separate class.

 

 

https://stackoverflow.com/questions/6812084/what-is-the-convention-of-means-in-php

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

 

All the PHP Magic methods begin with a double underscore.

728x90
반응형