PHP magic constant today saw a magic variable, which was never seen before, __dir __. I checked it and found it was added in php5.3. By the way, let's take a few examples, explain the magic variables of php
1 :__ FILE __
The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path.
This variable is the most widely used and probably the most widely used.
The web server will specify a documentroot, but different servers may have different documentroot settings. in this case, a website is migrated from one server to another, in this way, the website may not run due to different paths.
"; Echo _ FILE __; echo"
"; Echo dirname (_ FILE _); echo"
"; Echo dirname (_ FILE _);?>
Output:
C: \ Web \ Apache \ htdocs \ Magic \ 06 \
C: \ Web \ Apache \ htdocs \ Magic \ 06 \ test. php
C: \ Web \ Apache \ htdocs \ Magic \ 06
C: \ Web \ Apache \ htdocs \ Magic
2 :__ LINE __
The current row number in the file. This variable is useful when debugging errors occur. it is a personal opinion.
Output:
2
3 :__ CLASS __
Class name. The results returned by PHP5 are case sensitive.
"; } function say_b() { echo "'b' - said the " . get_class($this) . "
"; }}class derived_class extends base_class{ function say_a() { parent::say_a(); echo "'a' - said the " . __CLASS__ . "
"; } function say_b() { parent::say_b(); echo "'b' - said the " . get_class($this) . "
"; }}$obj_b = new derived_class();$obj_b->say_a();echo "
";$obj_b->say_b();?>
Result:
'A'-said the base_class
'A'-said the derived_class
'B'-said the derived_class
'B'-said the derived_class
Sometimes, we can use get_class instead of _ CLASS __
4 :__ FUNCTION _ and _ METHOD __
_ FUNCTION __: FUNCTION name. The result returned in php5 is case sensitive.
_ METHOD __: name of the function in the METHOD. the result returned in php5 is case sensitive.
What are the differences between the two methods?
"; echo __METHOD__; }}function good(){ echo __FUNCTION__; echo "
"; echo __METHOD__;}$test = new test();$test->a();echo "
";good();?>
Returned results:
A
Test:
Good
Good
Compared with an isolated FUNCTION, the two functions can retrieve the FUNCTION name. if it is a method in the class, __function _ can only retrieve the method name of the class, while _ METHOD _ does not only retrieve the METHOD name, but also the class name
5 :__ DIR __
The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0)
If you want to use _ DIR _ in versions earlier than 5.3, you can do this.
6 :__ NAMESPACE __
Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0)
7 :__ STATIC __
When you call the static method of class, the class name is returned, which is case sensitive. If it is called in inheritance, the inherited class name can be returned no matter whether it is defined in inheritance.
Turn: http://blog.51yip.com/php/1165.html