首页 > Web开发 > 详细

PHP单粒模式

时间:2017-06-21 17:56:59      阅读:349      评论:0      收藏:0      [点我收藏+]
<?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单粒模式

原文:http://www.cnblogs.com/isuben/p/7060769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!