Singleton::getInstance 既没被继承,也没被调用,那么cache是如何得到 它的实例的呢?$this->_handle = new StdClass(); 按我理解就是创建了一个空的对象,具体实现也无法明白
<?php
class Singleton
{
private static $_instances = array();
protected function __construct()
{
}
final public function __clone()
{
trigger_error("clone method is not allowed.", E_USER_ERROR);
}
final public static function getInstance()
{
$c = get_called_class();
if(!isset(self::$_instances[$c])) {
self::$_instances[$c] = new $c;
}
return self::$_instances[$c];
}
}
class Cache Extends Singleton
{
private $_handle = null;
protected function __construct()
{
$this->_connect();
}
private function _connect()
{
$this->_handle = new StdClass();
}
public function getHandle()
{
return $this->_handle;
}
}
1
hahasong 2015-04-02 21:53:43 +08:00
Cache::getInstance()会把$_handle赋值给$_instances['Cache']
都singleton了还继承干什么,写的shenmagui |