PHP单粒模式
<?php class C { //三私一公 protected static $_instance = null; protected function __construct() //protected方便继承 ,privated无法继承 { throw new Exception("禁止实例化"); } protected function __clone() { throw new Exception("禁止克隆") } public function getInstance() { if (static::$_instance === null) { static::$_instance = new static;//后期静态绑定,以实现继承 } return static::$_instance; } } class D extends C { protected static $_instance = null;//继承之后能够实现两套不同的数据库链接方式 } $c = C::getInstance(); $d = D::getInstance(); var_dump($c === $d);