PHP单粒模式

2022-10-16,

<?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);

PHP单粒模式的相关教程结束。

《PHP单粒模式.doc》

下载本文的Word格式文档,以方便收藏与打印。