魔术常数是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 your current line number i.e;3
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.
function test_function(){
echo 'hyiiii';
}
test_function();
echo __function__ . "<br><br>";//gives the blank 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.
}
}
class second extends first
{
public function __construct() {
;
}
}
$t = new second;
$t->test_first();
echo "<h3>example for __trait__</h3>";
trait created_trait{
function abc(){
echo __trait__;//will print name of the trait created_trait
}
}
class anew{
use created_trait;
}
$a = new anew;
$a->abc();
echo "<h3>example for __method__</h3>";
class meth{
public function __construct() {
echo __method__ . "<br><br>";//print meth::__construct
}
public function meth_fun(){
echo __method__;//print meth::meth_fun
}
}
$a = new meth;
$a->meth_fun();
echo "<h3>example for __namespace__</h3>";
class name{
public function __construct() {
echo 'this line will be printed on calling namespace';
}
}
$clas_name= __namespace__ .'\name';
$a = new $clas_name;
?>
执行上面代码得到以下结果 -