Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP magic constant


May 11, 2021 PHP


Table of contents


PHP magic constant

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

However, many constants are defined by different extension libraries and appear only when they are loaded, either dynamically or at compile time.

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

For example__LINE__ value of a script depends on the line in which it is in the script. These special constants are case insensescies and are as follows:


__LINE__

The current line number in the file.

Instance:

<?php
    echo '这是第 “ '  . __LINE__ . ' ” 行';
?>

The output of the above examples is:

这是第 “ 2 ” 行

__FILE__

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

Since PHP 4.0.2, the __FILE__ always contains an absolute path (or, in the case of a symbolic connection, a parsed absolute path), where as of this time the version sometimes contains a relative path.

Instance:

<?php
    echo '该文件位于 “ '  . __FILE__ . ' ” ';
?>

The output of the above examples is:

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

__DIR__

The directory where the file is located. If used in a included file, the directory in which the included file is located is returned.

It is equivalent to dirname (__FILE__). U nless it is the root, the name in the directory does not include the slash at the end. (New in PHP 5.3.0)

Instance:

<?php
    echo '该文件位于 “ '  . __DIR__ . ' ” ';
?>

The output of the above examples is:

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

__FUNCTION__

Function name (PHP 4.3.0 added). T his constant returns the first name (case sensitive) of the function when it was defined from PHP 5. This value is always lowercase in PHP 4.

Instance:

<?php
function test() {
    echo  '函数名为:' . __FUNCTION__ ;}test();
?>

The output of the above examples is:

函数名为:test

__CLASS__

The name of the class (PHP 4.3.0 new addition). This constant returns the first name (case sensitive) of the class when it was defined from PHP 5.

This value is always lowercase in PHP 4. T he class name includes its declared play area (for example, Foo?Bar). N ote that since PHP 5.4__CLASS__ also works on trait. When used in the trait method, the __CLASS__ name of the class that called the trait method.

Instance:

<?php
class test {
    function _print() {
    echo '类名为:'  . __CLASS__ . "<br>";
    echo  '函数名为:' . __FUNCTION__ ;
    }}$t = new test();$t->_print();
?>

The output of the above examples is:

类名为:test函数名为:_print

__TRAIT__

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

The Trait name includes its declared play area (for example, Foo?Bar).

Members inherited from the base class are overwritten by the MyHelloWorld method in SayWorld Trait inserted. I ts behavior is consistent with the methods defined in the MyHelloWorld class. The priority is that the method in the current class overrides the trait method, which in turn overrides the method in the base class.

<?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 routine outputs:

Hello World!

__METHOD__

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

Instance:

<?php
function test() {
    echo  '函数名为:' . __METHOD__ ;
}
test();
?>

The output of the above examples is:

函数名为:test

__NAMESPACE__

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

Instance:

<?php
namespace MyProject;
echo '命名空间为:"', __NAMESPACE__, '"'; // 输出 "MyProject"
?>

The output of the above examples is:

命名空间为:"MyProject"