MMTTMM
PHP 5.3允许通过后期静态绑定创建可继承的Singleton类: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;
}}这解决了这个问题,在PHP5.3之前,任何扩展Singleton的类都会生成它的父类的实例,而不是它自己的实例。现在你可以:class Foobar extends Singleton {};$foo = Foobar::getInstance();$foo将是Foobar的实例,而不是Singleton的实例。