php单例模式

写一个单例模式的demo

<?php
/**
 *  单例模式demo
 *  然而用处并不大
 */
class Single {
    private static  $instance;
    private function __construct(){
        return false;
    }
    /**
     * [getInstance description]
     * @return [type] [description]
     */
    final public function getInstance(){
        if(!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * 防止对象被克隆
     * @return [type] [description]
     */
    function __clone(){
        unset(self::$instance);
        return false;
    }
    /**
     * 防止被序列化
     * @return array [description]
     */
    function __sleep(){
        unset(slef::$instance);
        return false;
    }
    /**
     * 类的其他方法
     * @return [type] [description]
     */
    public function echosth(){
        echo "i am a single class";
    }

}

$obj = Single::getInstance();
$obj-> echosth();
Show Comments
备案信息: 京ICP备20002019号