
//单例模式,调用多次只new一次对象 class Test1{ protected static $tt; private function __construct(){} static function getInstance(){ if(self::$tt){ echo "对象已经创建<br>"; return self::$tt; }else { self::$tt = new Test1(); echo "创建对象<br>"; return self::$tt; } } function echoHello(){ echo "Hello<br>"; } } $test = Test1::getInstance(); $test->echoHello(); $test = Test1::getInstance(); $test->echoHello(); $test = Test1::getInstance(); $test->echoHello(); $test = Test1::getInstance(); $test->echoHello();文章来源: php单例模式
人吐槽 | 人点赞 |
发表评论