###模式定义 简单说来,单例模式的作用就是保证在整个应用程序的生命周期中,任何一个时刻,单例类的实例都只存在一个,同时这个类还必须提供一个访问该类的全局访问点。 常见使用实例:数据库连接器;日志记录器(如果有多种用途使用多例模式);锁定文件。 ###UML类图  ###示例代码 Singleton.php ```php <?php namespace App\Sheji\Singleton; /** * Singleton类 */ class Singleton { /** * @var Singleton reference to singleton instance */ private static $instance; /** * 通过延迟加载(用到时才加载)获取实例 * * @return self */ public static function getInstance() { if (null === static::$instance) { static::$instance = new static; } return static::$instance; } /** * 构造函数私有,不允许在外部实例化 * */ private function __construct() { } /** * 防止对象实例被克隆 * * @return void */ private function __clone() { } /** * 防止被反序列化 * * @return void */ private function __wakeup() { } } ```