_ FILE, CLASS, and other magic variables in PHP

Source: Internet
Author: User
The _ FILE ,__ CLASS and other magic variables in PHP today saw a magic variable, which was never seen before, __dir __. I checked it and found it was added by php5.3, by the way, let's take a few examples to explain the complete path and FILE name of the php magic variable 1 ,__ FILE. If it is used in a file to be included, the file name to be included is returned. From Magic variables such as _ FILE ,__ CLASS in PHP4.0 PHP

Today I saw a magic variable, which I have never seen before, __dir __. I checked it and found it was added in php5.3. By the way, I will explain the magic variable in 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. 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.

  1. /**?
  2. Set your root directory in your public configuration file, so you don't have to worry about moving frequently. ?
  3. */??
  4. Define ('root _ path ',? Dirname (_ FILE __)?.? DIRECTORY_SEPARATOR );??
  5. Echo? ROOT_PATH ;??
  6. Echo? "
    ";??
  7. Echo? _ FILE __;??
  8. Echo? "
    ";??
  9. Echo? Dirname (_ FILE __);??
  10. Echo? "
    ";??
  11. Echo? Dirname (_ FILE __));??
  12. ?> ??
 "; Echo _ FILE __; echo"
"; Echo dirname (_ FILE _); echo"
"; Echo dirname (_ FILE _);?>

2 ,__ LINE __

The current row number in the file. This variable is useful when debugging errors occur. it is a personal opinion.

  1. Echo? _ LINE __;?? // Display, __line _'s row number ??
  2. ?> ??
 

3 ,__ CLASS __

Class name. The results returned by PHP5 are case sensitive.

  1. Class? Base_class ??
  2. {??
  3. ? Function? Say_a ()??
  4. ? {??
  5. ? Echo? "'A '? -? Said? The? "?.? _ CLASS __?.? "
    ";??
  6. ?} ??
  7. ? Function? Say_ B ()??
  8. ? {??
  9. ? Echo? "'B '? -? Said? The? "?.? Get_class ($ this )?.? "
    ";??
  10. ?} ??
  11. }??
  12. ??
  13. Class? Derived_class? Extends? Base_class ??
  14. {??
  15. ? Function? Say_a ()??
  16. ? {??
  17. ? Parent: say_a ();??
  18. ? Echo? "'A '? -? Said? The? "?.? _ CLASS __?.? "
    ";??
  19. ?} ??
  20. ? Function? Say_ B ()??
  21. ? {??
  22. ? Parent: say_ B ();??
  23. ? Echo? "'B '? -? Said? The? "?.? Get_class ($ this )?.? "
    ";??
  24. ?} ??
  25. }??
  26. ??
  27. $ Obj_ B? =? New? Derived_class ();??
  28. $ Obj_ B-> say_a ();??
  29. Echo? "
    ";??
  30. $ Obj_ B-> say_ B ();??
  31. ?> ??
  32. The result is :??
  33. 'A '? -? Said? The? Base_class ??
  34. 'A '? -? Said? The? Derived_class ??
  35. ??
  36. 'B '? -? Said? The ?? Derived_class ??
  37. 'B '? -? Said? The? Derived_class ??
 ";} 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?

  1. Class? Test ??
  2. {??
  3. ? Function? A ()??
  4. ? {??
  5. ? Echo? _ FUNCTION __;??
  6. ? Echo? "
    ";??
  7. ? Echo? _ METHOD __;??
  8. ?} ??
  9. }??
  10. ??
  11. Function? Good? (){??
  12. ? Echo? _ FUNCTION __;??
  13. ? Echo? "
    ";??
  14. ? Echo? _ METHOD __;??
  15. }??
  16. ??
  17. $ Test? =? New? Test ();??
  18. $ Test-> ();??
  19. Echo? "
    ";??
  20. Good ();??
  21. ?> ??
  22. Returned result :??
  23. A ??
  24. Test: ??
  25. Good ??
  26. Good ??
 "; Echo _ METHOD __;}} function good () {echo _ FUNCTION __; echo"
"; Echo _ METHOD __;}$ test = new test (); $ test-> a (); echo"
"; Good () ;?> Returned result: atest: agoodgood

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.

  1. If (! Defined ('_ DIR __'))? {??
  2. ? $ IPos? =? Strrpos (_ FILE __,? "/");??
  3. ? Define ("_ DIR __",? Substr (_ FILE __,? 0 ,? $ IPos )?.? "/");??
  4. }??
  5. ?> ??
 

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.

  1. // Php5.3 ??
  2. Class? Model ??
  3. {??
  4. ? Public? Static? Function? Find ()??
  5. ? {??
  6. ? Echo? _ STATIC __;??
  7. ?} ??
  8. }??
  9. ??
  10. Class? Product? Extends? Model? {}??
  11. Class? User? Extends? Model? {}??
  12. ??
  13. Product: find ();? //? "Product "??
  14. User: find ();? //? "User "??
  15. ?> ??
 

?

Source:

Http://blog.51yip.com/php/1165.html

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.