单例模式应该是设计模式里最容易理解的设计模式了,它主要解决的就是资源浪费问题,一个类只允许一个实例化
class Database
{
protected static $db = null;
private function __construct()
{
}
public static function getInstance ()
{
if (is_null(self::$db)) {
self::$db = new self;
}
return self::$db;
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号