单例模式

来源:互联网 发布:led走字屏改字软件 编辑:程序博客网 时间:2024/06/03 18:57
<?php
/**
 * 单例模式,就指有多种用户new一个类,最后都只是调用一个类中的方法,节省资源
 *
 */
class single {
    public $hash;//输出随机码
    
    static protected $ins = null;
    
    final protected function __construct() {
        $this->hash = mt_rand(1,9999);
    }
    
    static public function getstance() {
        if (self::$ins instanceof self) {
            return self::$ins;
        }
        self::$ins = new self();
        return self::$ins;
    }
}

class a extends single {
    
}

$aa = a::getstance();
$bb = a::getstance();
var_dump($aa);
var_dump($bb);
?>
0 0