PHP教程
PHP Mysql
PHP面向对象
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魔法常量
下面用示例代码定义了所有常量:

1. __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

2. __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

3. __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

4. __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

5. __CLASS__:

它返回类名,这里使用了这个魔法常量。 __CLASS__ 常量也适用于特征。
示例:
<?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

6. __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

7. __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

8. __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.

9. ClassName::class:

这个神奇的常量不以双下划线(__) 开始和结束。它返回 ClassName 的完全限定名称。 PHP 5.5.0 中添加了 ClassName::class。它对命名空间类很有用。
示例:
<?php 
	namespace Technical_Portal;
	echo "<h3>Example for CLASSNAME::class </h3>";
	class lidihuo {  
	}
	echo lidihuo::class;    //ClassName::class 
?>
输出:

Example for ClassName::class

Technical_Portal\lidihuo
注意: 记住命名空间必须在脚本的第一条语句或任何声明调用之后,否则会产生致命错误。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4