PHP Magic Constants

Source: Internet
Author: User

PHP Magic Constants

PHP provides a large number of predefined constants to any script it runs.

However, many constants are defined by different extensions, and are only present when the extensions are loaded, either dynamically after loading, or they are included at compile time.

There are eight magic constants whose values change as they change position in the code.

For example, the value of __line__ depends on the row it is in the script to determine. These special constants are case-insensitive, as follows:

__line__

The current line number in the file.

<?php    Echo ' This is the first '  . __line__. ' The line ';? >

  

The result of the above example output is:

这是第 “ 2 ” 行
__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.

<?php    Echo ' This file is located in '  . __file__. ‘ ” ‘;? >

  

The result of the above example output is:

该文件位于 “ E:\wamp\www\test\index.php ”
__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)

<?php    Echo ' This file is located in '  . __dir__. ‘ ” ‘;? >

  

The result of the above example output is:

该文件位于 “ E:\wamp\www\test ”
__function__

The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.

<?phpfunction Test () {    echo  ' function is named: '. __function__;} Test ();? >

  

The result of the above example output is:

函数名为:test
__class__

The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive).

In PHP 4, this value is always in lowercase letters. The class name includes its declared action area (for example, Foo\bar). Note since PHP 5.4, __CLASS__ has also worked for trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.

<?phpclass Test {    function _print () {    echo ' class name is: '  . __class__. "<br>";    Echo  ' function is named: '. __function__;    }} $t = new test (); $t->_print ();? >

  

The result of the above example output is:

类名为:test函数名为:_print
__trait__

Trait's name (PHP 5.4.0 new Plus). Since PHP 5.4.0, PHP has implemented a method of code reuse, called traits.

The Trait name includes its declared function area (for example, Foo\bar).

Members inherited from the base class are overwritten by the Myhelloworld method in the inserted Sayworld Trait. Its behavior is consistent with the methods defined in the Myhelloworld class. The precedence is that methods in the current class override the Trait method, and the trait method overrides the method in the base class.

<?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 routines will output:

Hello World!
__method__

The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).

<?phpfunction Test () {    echo  ' function is named: '. __method__;} Test ();? >

  

The result of the above example output is:

函数名为:test


提示:__FUNCTION__仅返回函数名,__METHOD__返回类名和函数名
__namespace__

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

<?phpnamespace Myproject;echo ' namespace: ' ', __namespace__, ' '; Output "MyProject"?>

  

The result of the above example output is:

命名空间为:"MyProject"

PHP Magic Constants

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.