在PHP 5中创建单例设计模式如何使用PHP 5类创建Singleton类?
3 回答
MMTTMM
TA贡献1869条经验 获得超4个赞
class Singleton{
protected static $instance = null;
protected function __construct()
{
//Thou shalt not construct that which is unconstructable!
}
protected function __clone()
{
//Me not like clones! Me smash clones!
}
public static function getInstance()
{
if (!isset(static::$instance)) {
static::$instance = new static;
}
return static::$instance;
}}class Foobar extends Singleton {};$foo = Foobar::getInstance();- 3 回答
- 0 关注
- 653 浏览
添加回答
举报
0/150
提交
取消
