Heim > Artikel > Backend-Entwicklung > php 单例模式
class DB{ static private $_instance; public $name='name'; private function __construct(){ } static public function getInstance(){ if(!(self::$_instance instanceof self)){ self::$_instance = new self(); } return self::$_instance; } public function __clone(){ trigger_error('对象不允许被复制',E_USER_ERROR); } public function test(){ echo '调用方法成功'; } } //$db = new DB(); $db = DB::getInstance(); $db_clone = clone $db;