Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Magic Constants

PHP Magic Constants

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

However, many constants are defined by different extension libraries and only appear when those extensions are loaded, or after dynamic loading, or are already included at compile time.

There are eight magic constants whose values change depending on where they are used in the code.

For example LINE 's value depends on the line it is on in the script. These special constants are case-insensitive, as follows:


LINE

The current line number in the file.

Example

The output of the above example is:



这是第 “ 2 ” 行


FILE

The full path and filename of the file. If used in an included file, the name of the included file is returned.

Since PHP 4.0.2,FILE always contains an absolute path (if it is a symbolic link, it is the resolved absolute path), while versions before this sometimes contained a relative path.

Example:

Example

The output of the above example is:



该文件位于 “ E:\wamp\www\test\index.php ”


DIR

The directory where the file is located. If used in an included file, the directory of the included file is returned.

It is equivalent to dirname(FILE). Unless it is the root directory, the directory name does not include a trailing slash. (Added in PHP 5.3.0)

Example

The output of the above example is:



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


FUNCTION

The function name (added in PHP 4.3.0). Since PHP 5, this constant returns the name of the function as it was defined (case-sensitive). In PHP 4, this value is always lowercase.

Example

The output of the above example is:



函数名为:test


CLASS

The class name (added in PHP 4.3.0). Since PHP 5, this constant returns the name of the class as it was defined (case-sensitive).

In PHP 4, this value is always lowercase. The class name includes the namespace in which it was declared (e.g., Foo\Bar). Note that since PHP 5.4 CLASS works for traits as well. When used in a trait method,CLASS is the name of the class that called the trait method.

Example

" ; echo ' function name is: ' . __FUNCTION__ ; } } $t = new test ( ) ; $t -> _print ( ) ; ?>

The output of the above example is:



类名为:test

函数名为:_print


TRAIT

The name of the trait (added in PHP 5.4.0). Since PHP 5.4.0, PHP has implemented a method for code reuse called traits.

The trait name includes the namespace in which it was declared (e.g., Foo\Bar).

Members inherited from the base class are overridden by the MyHelloWorld method inserted by the SayWorld trait. Its behavior is consistent with the method defined in the MyHelloWorld class. The priority order is that methods in the current class override trait methods, and trait methods override methods in the base class.

Example

sayHello ( ) ; ?>

The above example will output:



Hello World!


METHOD

The class method name (added in PHP 5.0.0). Returns the name of the method as it was defined (case-sensitive).

Example:

Example

The output of the above example is:



函数名为:test


NAMESPACE

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

Example:

Example

The output of the above example is:



命名空间为:"MyProject"