亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

單例模式的實(shí)現(xiàn)

Original 2019-05-21 11:06:51 168
abstract:<?php //單例模式 class Test { } $test1 = new Test(); $test2 = new Test(); echo ($test1 instanceof Test)?'是':'否'; echo '<b
<?php
//單例模式
class Test
{

}
$test1 = new Test();
$test2 = new Test();

echo ($test1 instanceof Test)?'是':'否';
echo '<br>';
echo ($test2 instanceof Test)?'是':'否';
echo '<br>';
echo ($test1 === $test2)?'是':'否';
echo '<br>';
var_dump($test1,$test2);
class Tag
{
    //私有化構(gòu)造方法,防止外部實(shí)例化類
    private function __construct(){ }
    //私有化克隆方法,防止克隆類
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
    //創(chuàng)建一個(gè)類的內(nèi)部靜態(tài)方法,保存類的唯一實(shí)例
    protected static $instance = null;
    //創(chuàng)建一個(gè)外部接口,創(chuàng)建并返回類的唯一實(shí)例
    public static function getInstance()
    {
        if (is_null(self::$instance)){
            self::$instance = new static();
        }
        return self::$instance;
    }
}

$test3 = Tag::getInstance();
$test4 = Tag::getInstance();

echo ($test3 instanceof Tag)?'是':'否';
echo '<br>';
echo ($test4 instanceof Tag)?'是':'否';
echo '<br>';
echo ($test3 === $test4)?'是':'否';
echo '<br>';
var_dump($test3,$test4);


Correcting teacher:查無此人Correction time:2019-05-22 09:15:10
Teacher's summary:完成的不錯(cuò),php有很多設(shè)計(jì)模式,可以多了解。繼續(xù)加油。

Release Notes

Popular Entries