過去我認(rèn)為已經(jīng)記得很牢的知識(shí),今天也是模棱兩可。好記性,不如爛筆頭。
做筆記的意義在于,對(duì)模棱兩可的知識(shí)不斷的復(fù)盤,達(dá)到看到 蘋果,就想起 Apple , 就能想起
A-P-P-L-E的境界一般。
魔術(shù)常量
魔術(shù)常量(Magical Constants) 的值會(huì)伴隨著它們?cè)诖a中的位置改變而改變。
常用的 8 個(gè) PHP 魔術(shù)常量有:
| 名稱 | 說明 |
|---|---|
__LINE__ |
the current line number of the file |
__FILE__ |
the full path and filename of the file |
__DIR__ |
the directory of the file |
__NAMESPACE__ |
the name of the current namespace |
__CLASS__ |
the class name |
__FUNCTION__ |
the function name |
__METHOD__ |
the class method name |
__TRAIT__ |
The trait name. The trait name includes the namespace it was declared in |
源碼說明
<?php
namespace magic\consts;
echo __NAMESPACE__; // magic\consts
class Test extends Base
{
public function __construct()
{
parent::__construct();
var_dump(__CLASS__); //magic\consts\Test
}
public function echomethod()
{
var_dump(__METHOD__); // magic\consts\Test::echomethod
}
public function echofunction()
{
var_dump(__FUNCTION__); // echofunction
}
}
class Base
{
public function __construct()
{
var_dump(__CLASS__); //magic\consts\Base
}
}
$test = new Test();
$test->echomethod();
$test->echofunction();
var_dump(__DIR__); // F:\ProgramLife\WWW\Demo\magic_const
var_dump(__FILE__); // F:\ProgramLife\WWW\Demo\magic_const\const.php
var_dump(__LINE__); // 50