__CLASS__ get_class() get_called_class()區別

__CLASS__獲取當前的類名,this

get_class()與上面同樣,都是獲取當前的類名get

get_called_class()後期靜態綁定類的名稱io

class testA
{
    function printS()
    {
        echo 'testA' . __CLASS__ . '<br/>';
        echo 'testA' . get_class() . '<br/>';
        echo 'testA' . get_class($this) . '<br/>';
        echo 'testA' . get_called_class() . '<br/>';
        echo '=========' . '<br/>';
    }
}
class testB extends testA
{
    function printS()
    {
        parent::printS();
        echo 'testB' . __CLASS__ . '<br/>';
        echo 'testB' . get_class() . '<br/>';
        echo 'testB' . get_class($this) . '<br/>';
        echo 'testB' . get_called_class() . '<br/>';
    }
}function


$tb = new testB;
$tb->printS();class

結果:test

testAtestA
testAtestA
testAtestB
testAtestB
=========
testBtestB
testBtestB
testBtestB
testBtestBcall

單例實例get_called_class():static

public static function getInstance() {
        $class_name = get_called_class();
        if (isset(self::$instance[$class_name])) {
            return self::$instance[$class_name];
        }
        self::$instance[$class_name] = new $class_name;
        return self::$instance[$class_name];
    }sse

 

本篇是爲網上實例....單例

相關文章
相關標籤/搜索