PHP 单例模式

/**
 * Class Singleton 单例模式
 */
class Singleton{
    //私有静态属性
    private static $instance;
    //私有构造函数,防止new创建对象
    private function __construct()
    {
    }
    //防止对象被克隆
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    public static function getInstance() {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function test()
    {
        echo '单例模式';
    }
}

posted @ 2021-07-19 16:20  多云转晴啊,王桑  阅读(21)  评论(0编辑  收藏  举报