PHP 设计模式之数据对象映射模式
2017-08-07 13:42
639 查看
<?php // 数据对象映射模式 class User { public $id; public $name; public $mobile; public $regtime; private $dataFile; public function __construct($id) { $this->dataFile = "user.$id.data"; if(file_exists($this->dataFile)) { $data = json_decode(file_get_contents($this->dataFile), 1); $this->name = $data['name']; $this->mobile = $data['mobile']; $this->regtime = $data['regtime']; } } public function __destruct() { return file_put_contents($this->dataFile, json_encode([ 'name' => $this->name, 'mobile' => $this->mobile, 'regtime' => $this->regtime ])); } } // 结合注册器模式 class Register { private static $object; public static function get($key) { return isset(self::$object[$key]) ? self::$object[$key] : false; } public static function set($key, $obj) { self::$object[$key] = $obj; } } // 结合工厂模式 class Factory { public static function getUser($id) { if(!$user = Register::get($id)) { $user = new User($id); Register::set($id, $user); echo "register set \n"; } return $user; } } class Page { public function index() { $user = Factory::getUser(1); $user->mobile = '15165854585'; $user->name = 'test'; $user->regtime = time(); } public function test() { $user = Factory::getUser(1); $user->mobile = '15165854585'; $user->name = 'test23333'; $user->regtime = time(); } } $page = new Page; $page->index(); $page->test();
相关文章推荐
- php设计模式 数据对象映射模式
- PHP 设计模式 笔记与总结(9)数据对象映射模式
- 慕课网----大话PHP设计模式 四(适配器模式,数据对象映射模式,观察者模式)
- 浅析php设计模式之数据对象映射模式
- php设计模式--数据对象映射模式
- PHP设计模式之数据对象映射模式
- PHP 设计模式 笔记与总结(10)数据对象映射模式 2
- 浅析php设计模式之数据对象映射模式
- PHP设计模式--数据访问对象模式
- PHP设计模式系列 - 数据访问对象模式
- PHP数据对象映射模式与实现简单的链式操作
- PHP设计模式之适配器模式,建造者模式,数据访问对象模式
- PHP设计模式之适配器模式,建造者模式,数据访问对象模式
- PHP设计模式 三 (适配器 策略 数据映射)
- PHP设计模式 数据访问对象模式
- PHP设计模式--数据访问对象模式
- php 设计模式-数据映射模式(应用程序与数据库交互模式)
- PHP设计模式系列 - 数据访问对象模式
- php 设计模式-数据映射模式(应用程序与数据库交互模式)
- PHP数据对象映射模式