PHP 魔术常量
魔术常量是在PHP中预定义的常量,根据使用情况而发生变化。它们以双下划线(__)开头和结尾。
它们与其他预定义常量类似,但由于它们根据上下文变化值,所以被称为 魔术 常量。
PHP中有 九个 魔术常量。其中八个魔术常量以双下划线(__)开始和结尾。
- __LINE__
- __FILE__
- __DIR__
- __FUNCTION__
- __CLASS__
- __TRAIT__
- __METHOD__
- __NAMESPACE__
- ClassName::class
所有常量在编译时解析,而不是运行时,与普通常量不同。魔术常量是不区分大小写的。
更新日志
| 版本 | 描述 | 
|---|---|
| 5.3.0 | 添加了 __DIR__和__NAMESPACE__魔术常量 | 
| 5.4.0 | 添加了 __TRAIT__魔术常量 | 
| 5.5.0 | 添加了::class魔术常量 | 
以下是所有常量的定义,附带示例代码:
__LINE__
它返回当前文件中使用此常量时的行号。
示例:
<?php 
    echo "<h3>Example for __LINE__</h3>";  
    // print Your current line number i.e;4   
    echo "You are at line number " . __LINE__ . "<br><br>";
?>
输出:
### Example for __LINE__
You are at line number 4
__FILE__
这个魔术常量返回执行的文件的完整路径,即文件所在位置。如果在include语句内部使用,将返回被包含的文件的名称。
示例:
<?php 
    echo "<h3>Example for __FILE__</h3>";  
    //print full path of file with .php extension  
    echo __FILE__ . "<br><br>";
?>
输出:
### Example for __FILE__
D:\xampp\htdocs\program\magic.php
__DIR__
它返回执行文件的完整目录路径。该魔术常量返回的路径相当于dirname(__FILE__)。除非是根目录,否则该魔术常量不会有尾部斜杠。
示例:
<?php 
    echo "<h3>Example for __DIR__</h3>";  
    //print full path of directory where script will be placed  
    echo __DIR__ . "<br><br>";
    //below output will equivalent to above one.
    echo dirname(__FILE__) . "<br><br>";  
?>
输出:
### Example for __DIR__
D:\xampp\htdocs\program
D:\xampp\htdocs\program
__FUNCTION__
这个魔术常量返回使用它的函数名称。如果在任何函数外部使用它,它将返回空。
示例:
<?php 
    echo "<h3>Example for __FUNCTION__</h3>";  
    //Using magic constant inside function.  
    function test(){  
        //print the function name i.e; test. 
        echo 'The function name is '. __FUNCTION__ . "<br><br>"; 
    }  
    test();  
    //Magic constant used outside function gives the blank output.  
    function test_function(){  
        echo 'Hie';  
    }  
    test_function();  
    //give the blank output. 
    echo  __FUNCTION__ . "<br><br>";
?>
输出:
### Example for __FUNCTION__
The function name is test
Hie
__CLASS__
它返回使用这个魔术常量的类名。__CLASS__常量在traits中也可用。
示例:
<?php 
    echo "<h3>Example for __CLASS__</h3>";  
    class JTP  
    {  
        public function __construct() {  
            ;  
    }  
    function getClassName(){  
        //print name of the class JTP. 
        echo __CLASS__ . "<br><br>"; 
        }  
    }  
    t = new JTP;t->getClassName();  
    //in case of multiple classes 
    class base
    {  
    function test_first(){  
            //will always print parent class which is base here.  
            echo __CLASS__; 
        }  
    }  
    class child extends base  
    {  
        public function __construct() {  
            ;  
        }  
    }  
    t = new child;t->test_first();  
?>
输出结果:
### Example for __CLASS__
JTP
base
__TRAIT__
这个魔术常量返回使用它的特性名称。
示例:
<?php 
    echo "<h3>Example for __TRAIT__</h3>";  
    trait created_trait {  
        function jtp(){  
            //will print name of the trait i.e; created_trait  
            echo __TRAIT__;
        }  
    }  
    class Company {  
        use created_trait;  
        }  
    a = new Company;a->jtp();  
?>
输出:
### Example for __TRAIT__
created_trait
__METHOD__
它返回包含这个魔法常量的类方法的名称。方法名返回的是声明时的名称。
示例:
<?php 
    echo "<h3>Example for __METHOD__</h3>";
    class method {  
        public function __construct() {  
            //print method::__construct  
                echo __METHOD__ . "<br><br>"; 
            }  
        public function meth_fun(){  
            //print method::meth_fun  
                echo __METHOD__; 
        }  
    }  
    a = new method;a->meth_fun();
?>
输出:
### Example for __METHOD__
method:: construct
method:: meth_fun
__NAMESPACE__
它返回当前命名空间的名称。
示例:
<?php 
    echo "<h3>Example for __NAMESPACE__</h3>";
    class name {  
        public function __construct() {  
            echo 'This line will print on calling namespace.';   
        }   
    }  
    class_name = __NAMESPACE__ . '\name';a = new class_name; 
?>
输出:
### Example for __NAMESPACE__
This line will print on calling namespace.
ClassName::class
这个魔术常量不是以双下划线(__)开头和结尾。它返回ClassName的完全合格名称。ClassName::class添加在 PHP 5.5.0 中。它在命名空间类中很有用。
示例:
<?php 
    namespace Technical_Portal;
    echo "<h3>Example for CLASSNAME::CLASS </h3>";
    class javatpoint {  
    }
    echo javatpoint::class;    //ClassName::class 
?>
输出:
### Example for ClassName::class
Technical_Portal\javatpoint
注意:记住命名空间必须是最前面的语句或在脚本中的任何声明调用之后,否则会产生致命错误。
 极客笔记
极客笔记