首页 >后端开发 >php教程 >php实现注册器+工厂+单例模式

php实现注册器+工厂+单例模式

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-20 12:46:27921浏览

<?php//注册器模式class Register{    protetcted static $objects;    //声明一个数组    static function set($alias,$objects){        self::objects[$alias]=$objects;    }    //销毁对象    function _unset($name){        unset($objects[$name]);    }    staitc function get($name){    return self::$objects[$name];    }}//单例模式class database{    protected static $db;    static function getInstance(){        if(self::$db){        //如果对象存在则使用,不存在创建        return self::$db;        }else{        return self::$db = new database();        }    }}//工厂模式class Factory{static function createDatabase(){            //首先获取到创建的对象    $db = database::getInstance();    $db->set('db1',$db);    return  $db;}}$db =Factory::createDatabase();



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn