<code>static private $_instance = NULL; </code>
在类的实例不存在的时候,这个方法会创建一个类的实例,并且返回这个实例。通常情况下,这个方法的名字叫getinstance
<code>public function getInstance()
{
if (self::$_instance == NULL) {
self::$_instance = new SingleTon();
}
return self::$_instance;
}
</code>如果用户试图使用new或者_clone去创建一个类的新对象,就会破坏单例模式的限制。所以还需要声明这两个方法为private.
<code>private function __construct(){
}
private function __clone(){
}
</code>版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了php-single pattern,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号