PHP Trick Constants

Source: Internet
Author: User
PHP Magic Constants

1,__file__

The full path and file name of the file. If used in the included file, returns the file name that is included. From PHP 4.0.2,__file__? Always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path.
This variable, I use the most, the estimate is also the most used by the people.

The Web server will specify a documentroot, but different servers, the settings of the DocumentRoot may be different, in this case, to move a Web site from one server to another server, it is possible because the path is different, resulting in the site can not run up.

 
  ";  echo __file__;  echo "
"; echo dirname (__file__); echo "
"; Echo dirname (DirName (__file__)); ? >
? 2,__line__

The current line number in the file.

 
  
? 3,__class__

The name of the PHP5 class, and the results returned 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, results returned in PHP5 are case-sensitive
__METHOD__: The function name in the method, the result returned in PHP5 is case-sensitive

Two is the name of the method of acquisition, what is the difference?

 
  ";   echo __method__;   }  }    function Good () {   echo __function__;   echo "
"; echo __method__; } $test = new test (); $test->a (); echo "
"; Good (); ? > return Result: a test::a good
? Compared to the isolated function, two can take out the function name, no difference, if it is a method in class, __function__ can only remove the class method name, and __method__ not only can take out the method name, but also can remove the class name

5,__dir__

The directory where the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0)

If you want to use __dir__ in a previous version of 5.3, you can

 
  
? 6,__namespace__

The name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 new)

7,__static__

When you call the class's static method, the class name is returned, which is case-sensitive. If called in inheritance, the inherited class name can be returned regardless of whether there is a definition in the 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.