魔术常量是PHP中预定义常量,根据它们的使用而发生改变。它们以双下划线(__)开头,以双下划线结尾。
它们类似于其他预定义的常量,但是它们随着上下文的改变他们的值,它们成为魔术常量。
下表中定义了八个魔术常量。它们不区分大小写。

常量名称 描述
__LINE__ 表示使用当前行号
__FILE__ 表示文件的完整路径和文件名。如果他在include中使用,则返回包含文件的名称
__DIR__ 表示文件的完整目录路径。等同与dirname(__file__)。除非它是根目录,否则他没有尾部斜杠。他还解析符号链接
__FUNCTION__ 表示使用它的函数名称。如果它在任何函数之外使用,则它将返回空白。
__CLASS__ 表示使用它的函数名称。如果它在任何函数之外使用,则它将返回空白。
__TRAIT__ 表示使用它的特征名称。如果它在任何函数之外使用,则它将返回空白。它包括它被声明的命名空间。
__METHOD__ 表示使用它的类方法的名称。方法名称在有声明时返回。
__NAMESPACE__ 表示当前命名空间名称。

实例

文件名:magic.php

<?php
   echo "<h3>Example for __LINE__</h3>
   echo "You are at line number ". __LINE__."<br><br>";
   //print you current line number 
   
   echo "<h3>Example for __File__ </h3>";
   echo __FILE__."<br><br>";
   //print full path of file with .php extension
   echo "<h3>Example for __DIR__</h3>";
   echo __DIR__."<br><br>";
   //print full path of directory where script will be placed
   echo dirname(__FILE__) . "<br><br>";
   //its output is equivalent to above one.
   echo "<h3>Example for __FUNCTION__</h3>";
   //Using magic constant inside function.
   
   function cash(){
      echo 'the function name is '.__FUNCTION__."<br><br>";
      //the function name is cash.
   }
   cash();
   
   //Using magic constant outside function gives the blank output.
   
   test _function();
   echo __FUNCTION__."<br><br>";
   //gives the black output.
   
   echo "<h3> Example for __CLASS__</h3>";
   
   class abc{
      public function __construct(){
         ;
      }
      
      function abc_method(){
         echo __CLASS__."<br><br>";//print name of the class abc.
      }
   }
   
   $t = new abc;
   $t -> abc_method();
   
   class first{
      function test_first(){
         echo _CLASS_;
         //will always print parent class which is first here
      }
   }