Home >Backend Development >PHP Tutorial >Understanding and application of relevant knowledge about PHP magic variables
PHP Magic variables play an important role in PHP. This article explains them in detail.
PHP provides a large number of predefined constants to any script it runs.
However, many constants are defined by different extension libraries and will only appear when these extension libraries are loaded, or after dynamic loading, or have been included during compilation.
There are eight magic constants whose values change depending on their position in the code.
For example, the value of __LINE__ depends on the line it is located in the script. These special constants are not case-sensitive and are as follows:
__LINE__
The current line number in the file.
Example
f7ee39dd24b41f4236eb027c99203856
The output result of the above example is:
This is the full path and file name of the file in line "2"
__FILE__
. If used within an included file, returns the name of the included file.
Since PHP 4.0.2, __FILE__ always contains an absolute path (or the resolved absolute path in the case of a symbolic link), while versions before that sometimes contained a relative path.
Example:
Example
6d874ab7a53fad21ab5aad6f7a7d2837
The above example output The result is:
The file is located in the directory where the "E:\wamp\www\test\index.php"
<?phpfunction test() { echo '函数名为:' . __FUNCTION__ ;}test();?>The output result of the above example is: The function name is: test
<?phpclass test { function _print() { echo '类名为:' . __CLASS__ . "<br>"; echo '函数名为:' . __FUNCTION__ ; }}$t = new test();$t->_print();?>The output result of the above example is: Class name: test Function name: _print
<?phpclass Base { public function sayHello() { echo 'Hello '; }} trait SayWorld { public function sayHello() { parent::sayHello(); echo 'World!'; }} class MyHelloWorld extends Base { use SayWorld;} $o = new MyHelloWorld();$o->sayHello();?>The above routine will output: Hello World!
<?phpfunction test() { echo '函数名为:' . __METHOD__ ;}test();?>The output result of the above example is: The function name is: test
<?phpnamespace MyProject; echo '命名空间为:"', __NAMESPACE__, '"'; // 输出 "MyProject"?>The output result of the above example is: The namespace is: "MyProject"
What are the differences between php magic variables __METHOD__ and __FUNCTION__
php magic variables and magic function examples Tutorial summary
Detailed explanation of php magic variable usage examples, detailed explanation of php magic examples_PHP tutorial
The above is the detailed content of Understanding and application of relevant knowledge about PHP magic variables. For more information, please follow other related articles on the PHP Chinese website!