PHP Magic Constants
PHP provides a large number of predefined constants to any script it runs.
However, many of these constants are defined by different extension libraries and only appear when these libraries are loaded, either dynamically or after they have been compiled in.
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 it is on in the script. These special constants are case-insensitive, as follows:
__LINE__
The current line number of the file.
Example
<?php
echo 'This is line " ' . __LINE__ . ' "';
?>
The above example will output:
This is line “ 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's a symbolic link, it's the resolved absolute path), whereas earlier versions sometimes contained a relative path.
Example
<?php
echo 'This file is located at " ' . __FILE__ . ' "';
?>
The above example will output:
This file is located at “ E:\wamp\www\test\index.php ”
__DIR__
The directory of the file. If used in an included file, the directory of the included file is returned.
It is equivalent to dirname(__FILE__)
. The directory name does not include a trailing slash unless it is the root directory. (Added in PHP 5.3.0)
Example
<?php
echo 'This file is located at " ' . __DIR__ . ' "';
?>
The above example will output:
This file is located at “ E:\wamp\www\test ”
__FUNCTION__
The function name (added in PHP 4.3.0). Since PHP 5, this constant returns the function name as it was defined (case-sensitive). In PHP 4, the value is always in lowercase.
Example
<?php
function test() {
echo 'The function name is: ' . __FUNCTION__;
}
test();
?>
The above example will output:
The function name is: test
__CLASS__
The class name (added in PHP 4.3.0). Since PHP 5, this constant returns the class name as it was defined (case-sensitive).
In PHP 4, the value is always in lowercase. The class name includes the namespace it was declared in (e.g., Foo\Bar
). Note that since PHP 5.4, __CLASS__
also works in traits. When used in a trait method, __CLASS__
is the name of the class the trait is used in.
Example
<?php
class test {
function _print() {
echo 'The class name is: ' . __CLASS__ . "<br>";
echo 'The function name is: ' . __FUNCTION__;
}
}
$t = new test();
$t->_print();
?>
The above example will output:
The class name is: test
The function name is: _print
__TRAIT__
The trait name (added in PHP 5.4.0). Since PHP 5.4.0, PHP implemented a method of code reuse called traits.
The trait name includes the namespace it was declared in (e.g., Foo\Bar
).
Inherited members from the base class are overridden by methods inserted by the SayWorld
trait into MyHelloWorld
. The behavior is the same as if the method was defined in MyHelloWorld
. The priority order is that methods from the current class override trait methods, which in turn override methods from the base class.
Example
<?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 class method name (added in PHP 5.0.0). Returns the method name as it was defined (case-sensitive).
Example
<?php
function test() {
echo 'The method name is: ' . __METHOD__;
}
test();
?>
The above example will output:
The method name is: test
__NAMESPACE__
The name of the current namespace (case-sensitive). This constant is defined at compile time (introduced in PHP 5.3.0).
Example:
Example
<?php
namespace MyProject;
echo 'The namespace is: "', __NAMESPACE__, '"'; // Outputs "MyProject"
?>
The output of the above example is:
The namespace is: "MyProject"