Home > Backend Development > PHP Tutorial > php 单例模式

php 单例模式

WBOY
Release: 2016-06-20 12:33:08
Original
850 people have browsed it

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;
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template