php的单例模式

单例模式就是说一个例只能实例化一次,避免内存的占用 资源的节省,避免重复实例化
比如一个config类,多处用到,修改,避免没有实时更新
数据库连接, 一个就够了

Class Test{
    private static $instance;
    private static __construct(){}
    private static __clone(){}
    public static function instance(){
        if (!self::$instance instanceof self){
            self::$instance = new self();
        }
        return self::$instance;
    }
}

instanceof 使用这个关键字可以确定一个对象是类的实例、类的子类,还是实现了某个特定接口

原文地址:https://www.cnblogs.com/djwhome/p/12532800.html