PHP magic constant

Source: Internet
Author: User
PHP magic constant 1 ,__ complete path and FILE name of FILE. If it is used in a file to be included, the file name to be included is returned. Starting from PHP4.0.2, __file __? It always contains an absolute path (if it is a symbolic link, it is the absolute path after resolution), and the previous version sometimes contains a relative path. This variable is the most widely used and probably the most widely used. The web server will specify a docume PHP magic constant.

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. Since PHP 4.0.2,_ FILE __? It always contains an absolute path (if it is a symbolic link, it is the absolute path after resolution), and the previous version sometimes contains 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 _);?>
? 2 ,__ LINE __

The current row number in the file.

  
? 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
? 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 result: a test: a 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.

 
?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.