设计模式-单例 By SR.李 On 2019-10-08 设计模式 单例 单例的引用场景 针对一次请求类多次实例化 即,单次请求多次实例化对象,应只实例化一次 <?php class singleDemo { private static $instance = null; private function __construct() { // 代码,如连接数据库等 // ... } public static function getInstance() { if (null === self::$instance) { self::$instance = new self(); } return self::$instance; } } PHPCopy