PHP 魔術(shù)常量

過去我認(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 有 8 個(gè)常量(英文版文檔稱之為 magical constant),它們的值在它們被使用之前不是絕對(duì)確定的,需要...
    咚門閱讀 325評(píng)論 0 0
  • 有八個(gè)魔術(shù)常量它們的值隨著它們?cè)诖a中的位置改變而改變,這些特殊的常量不區(qū)分大小寫。 __LINE__ 文件中的當(dāng)...
    葉惜朝閱讀 695評(píng)論 0 0
  • PHP有八個(gè)魔術(shù)常量,它們的值隨著它們?cè)诖a中的位置改變而改變。這些特殊的常量不區(qū)分大小寫,如下:
    NoFacePeace閱讀 247評(píng)論 0 0
  • 1、 __LINE__ 獲取文件中的當(dāng)前行號(hào) 2、 __FILE__ 獲取文件的完整路徑和文件名,如果用在...
    yuzhan550閱讀 174評(píng)論 0 0
  • __LINE__ 文件中的當(dāng)前行數(shù) __FILE__ 當(dāng)前文件的絕對(duì)路徑,包括當(dāng)前文件的文件名 __DIR__ 當(dāng)...
    周瑜君閱讀 207評(píng)論 0 2

友情鏈接更多精彩內(nèi)容