我们经常采用如下方式定义单列:
class Singleton {
private static $instance = NULL;
/** 不容许直接调用构造函数 */
private function __construct() {
}
/** 不容许深度复制 */
private function __clone() {
}
public static function getInstance() {
if (NULL === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
}
很多人都会记得对深度copy的保护, 但, 其实我们却疏忽了一点...
with 15 Comments