剛才試了一下才發現,原來 PHP Class 裡的關鍵字 self
不管是大寫(SELF), 小寫(self), 還是大小寫混雜(SElf) 都是可以正常執行的
不過似乎大部份習慣的寫法是小寫,總之就只是一個冷知識。

以下是測試的程式碼:

<?php
Class TestClass
{
    const MSG = "Hello";
    public function test()
    {
        echo "self::MSG = " . self::MSG. "<br>";
        echo "SELF::MSG = " . SELF::MSG. "<br>";
        echo "SElf::MSG = " . SElf::MSG. "<br>";
    }
}

TestClass::test();

/**
 * 執行結果
 * self::MSG = Hello
 * SELF::MSG = Hello
 * SElf::MSG = Hello
 */

 

arrow
arrow
    全站熱搜

    wbkuo 發表在 痞客邦 留言(1) 人氣()