php 注册器模式 工厂模式
<?php /** * 注册器模式 * 全局共享和交换对象 */ class Register { public static $objects; // 定义全局数组 // 保存对象到全局数组 public static function set($name,$object) { self::$objects[$name] = $object; } // 获取某个对象 public static function get($name) { if (!isset(self::$objects[$name])){ return false; } return self::$objects[$name]; } // 获取全部对象 public static function getAll() { return self::$objects; } // 删除指定对象 public function remove($name) { unset(self::$objects[$name]); } } class Db { public function say() { echo 444444444444; } } class Factory { public static function getDb() { $db = new Db(); Register::set(‘db‘,$db); return $db; } } Factory::getDb(); $db = Register::get(‘db‘); var_dump($db->say());
https://zhuanlan.zhihu.com/p/110541976
https://cloud.tencent.com/developer/article/1556340
https://www.cnblogs.com/rxbook/p/10452582.html
相关推荐
yangyujun 2020-08-21
缘起宇轩阁 2019-12-30
swimmer000 2019-12-30
王志龙 2019-12-26
gougouzhang 2019-12-13
徐建岗网络管理 2011-12-18
HuangXiaoChuan 2019-03-20
一个过客 2019-03-25
machao 2019-03-21
cyAspNet 2019-07-01
Triones 2019-07-01
HavenZhao 2019-07-01
chengrile 2014-10-26
programisaart 2019-06-28
fuziwang 2019-06-28
gotea 2012-09-13