单例模式的四个要点
1 静态私有属性,保存当前实例
2 构造方法私有化,防止外部new来创建实例
3 克隆方法私有化,禁止从外部克隆生成本类实例
4 生成当前类的唯一实例
class single
{
private static $instance = null;
private function __construct()
{
}
private function __clone()
{
}
public static function getInstance
{
if (!self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号