PHP 魔術常量
魔術常量是在PHP中預定義的常量,根據(jù)使用情況而發(fā)生變化。愛掏網(wǎng) - it200.com它們以雙下劃線(__
)開頭和結尾。愛掏網(wǎng) - it200.com
它們與其他預定義常量類似,但由于它們根據(jù)上下文變化值,所以被稱為 魔術 常量。愛掏網(wǎng) - it200.com
PHP中有 九個 魔術常量。愛掏網(wǎng) - it200.com其中八個魔術常量以雙下劃線(__
)開始和結尾。愛掏網(wǎng) - it200.com
__LINE__
__FILE__
__DIR__
__FUNCTION__
__CLASS__
__TRAIT__
__METHOD__
__NAMESPACE__
- ClassName::class
所有常量在編譯時解析,而不是運行時,與普通常量不同。愛掏網(wǎng) - it200.com魔術常量是不區(qū)分大小寫的。愛掏網(wǎng) - it200.com
版本 | 描述 |
---|---|
5.3.0 | 添加了 __DIR__ 和 __NAMESPACE__ 魔術常量 |
5.4.0 | 添加了 __TRAIT__ 魔術常量 |
5.5.0 | 添加了::class魔術常量 |
以下是所有常量的定義,附帶示例代碼:
__LINE__
它返回當前文件中使用此常量時的行號。愛掏網(wǎng) - it200.com
示例:
<?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__
這個魔術常量返回執(zhí)行的文件的完整路徑,即文件所在位置。愛掏網(wǎng) - it200.com如果在include語句內部使用,將返回被包含的文件的名稱。愛掏網(wǎng) - it200.com
示例:
<?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__
它返回執(zhí)行文件的完整目錄路徑。愛掏網(wǎng) - it200.com該魔術常量返回的路徑相當于dirname(__FILE__)
。愛掏網(wǎng) - it200.com除非是根目錄,否則該魔術常量不會有尾部斜杠。愛掏網(wǎng) - it200.com
示例:
<?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__
這個魔術常量返回使用它的函數(shù)名稱。愛掏網(wǎng) - it200.com如果在任何函數(shù)外部使用它,它將返回空。愛掏網(wǎng) - it200.com
示例:
<?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__
它返回使用這個魔術常量的類名。愛掏網(wǎng) - it200.com__CLASS__
常量在traits中也可用。愛掏網(wǎng) - it200.com
示例:
<?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__
這個魔術常量返回使用它的特性名稱。愛掏網(wǎng) - it200.com
示例:
<?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__
它返回包含這個魔法常量的類方法的名稱。愛掏網(wǎng) - it200.com方法名返回的是聲明時的名稱。愛掏網(wǎng) - it200.com
示例:
<?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__
它返回當前命名空間的名稱。愛掏網(wǎng) - it200.com
示例:
<?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
這個魔術常量不是以雙下劃線(__
)開頭和結尾。愛掏網(wǎng) - it200.com它返回ClassName的完全合格名稱。愛掏網(wǎng) - it200.comClassName::class添加在 PHP 5.5.0 中。愛掏網(wǎng) - it200.com它在命名空間類中很有用。愛掏網(wǎng) - it200.com
示例:
<?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
注意:記住命名空間必須是最前面的語句或在腳本中的任何聲明調用之后,否則會產(chǎn)生致命錯誤。愛掏網(wǎng) - it200.com