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 these libraries are loaded, either after dynamic loading or when they have already been included at compile time.

There are eight magic constants whose values change depending on their position in the code.

For example, the value of __LINE__ depends on the line in the script where it is used. These special constants are case-insensitive, as follows:


__LINE__

The current line number in the file.

Examples

<?php echo 'This is line "' . __LINE__ . '"'; ?>

The output of the above example is:

这是第 “ 2 ” 行

__FILE__

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

As of PHP 4.0.2, __FILE__ always contains an absolute path (if it is a symbolic link, it is the resolved absolute path), while in versions before that it sometimes contained a relative path.

Example:

Examples

<?php echo 'The file is located at "' . __FILE__ . ' " '; ?>

The output of the above example is:

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

__DIR__

The directory of the file. If used inside an included file, the directory of the included file is returned.

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

Examples

<?php echo 'The file is located at "' . __DIR__ . ' " '; ?>

The output of the above example is:

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

__FUNCTION__

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

Examples

<?php function test() { echo 'Function name:' . __FUNCTION__ ; } test(); ?>

The output of the above example is:

函数名为:test

__CLASS__

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

In PHP 4, the value was always lowercase. The class name includes its declared scope (e.g., Foo\Bar). Note that as of PHP 5.4, __CLASS__ also works with traits. When used in a trait method, __CLASS__ is the name of the class calling the trait method.

Examples

<?php class test { function _print() { echo 'Class name:' . __CLASS__ . "<br>"; echo 'Function name:' . __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). As of PHP 5.4.0, PHP implements a method of code reuse called traits.

The Trait name includes its declared scope (e.g., Foo\Bar).

Members inherited from the base class are overridden by the MyHelloWorld method inserted from 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.

Examples

<?php class 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 example will output:

Hello World!

__METHOD__

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

Example:

Examples

<?php function test() { echo 'Function name:' . __METHOD__ ; } test(); ?>

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:

Examples

<?php namespace MyProject; echo 'The namespace is: "', __NAMESPACE__, '"'; //Outputs "MyProject" ?>

The output of the above example is:

命名空间为:"MyProject"
Other Extensions