2020-12-29 PHP面向对象中单例模式 PHP面向对象中单例模式 <?php class Single { public $rand; public static $ob; //final 方法不能被子类重写,实现单例模式 final protected function __construct() { $this->rand = mt_rand(1000, 9999); } public static function getins() { if (Single::$ob == null) { Single::$ob = new Single(); } return Single::$ob; } } var_dump(Single::getins()); var_dump(Single::getins()); 2020年12月29日 665 阅读 0 评论