[{"data":1,"prerenderedAt":63},["ShallowReactive",2],{"article-29":3},{"code":4,"msg":5,"data":6,"count":15},200,"查询成功",{"id":7,"title":8,"keywords":9,"description":10,"category_id":11,"content":12,"body_html":13,"thumb_up":11,"clicks":14,"sort":15,"remark":16,"status":17,"is_open":17,"is_deleted":15,"is_top":15,"is_recommend":15,"create_time":18,"update_time":19,"image_id":20,"url":13,"member_id":15,"cate_name":21,"prev":22,"next":25,"tags":28,"words":34,"read_time":35,"comments":15,"cover":36,"relevant":37},29,"PHP设计模式之迭代器、代理模式","php,设计模式,迭代器,代理","本文章讲述了php设计模式中的迭代器模式和代理模式，以及简单的应用实例",2,"一、迭代器模式\n1. 迭代器模式，在不需要了解内部实现的前提下，遍历一个聚合对象的内部元素\n2. 相比传统的编程模式，迭代器模式可以隐藏遍历元素所需的操作\n\n应用场景\n\n遍历数据库表，拿到所有的user对象，然后用佛 foreach 循环，在循环的过程中修改某些字段的值。\n\n实例代码：\n```php\nclass MySQLis{\n    protected $conn;\n    public function connect($host, $user, $pwd, $dbname){\n        $this->conn = mysqli_connect($host, $user, $pwd, $dbname);\n    }\n    public function query($sql){\n        $res =mysqli_query($this->conn, $sql);\n    return $res;\n    }\n    public function close(){\n    mysqli_close($this->conn);\n    }\n}\nclass User{\u002F\u002F数据对象模式\n    public $id;\n    public $name;\n    public $tell;\n    protected $db;\n    public function __construct($id){\n    $this->db = new MySQLis();\n    $this->db->connect('localhost', 'func', 'passwd', 'test');\n    $res = $this->db->query(\"select * from user where id = {$id} limit 1\");\n    $data = $res->fetch_assoc();\n    $this->id = $data['id'];\n    }\n    public function __destruct(){\n    $this->db->query(\"update user set name = '{$this->name}', tell = '{$this->tell}' where id= {$this->id}\");\n    }\n}\nclass Factory{\u002F\u002F工厂模式\n    public static function CreateAnimal($name){\n    }\n    public static function getUser($id){\n    $key = \"user_{$id}\";\n    $user = Register::get($key);\n    if (!$user) {\n        $user = new User($id);\n        Register::set($key, $user);\n    }\n    return $user;\n    }\n}\nclass Register {\u002F\u002F注册器模式\n    protected static $object;\n    public static function set($alias, $object){\n    self::$object[$alias] = $object;\n    }\n    public static function get($alias){\n    return self::$object[$alias];\n    }\n    public static function _unset($alias){\n    unset(self::$object[$alias]);\n    }\n}\nclass AllUser implements \\Iterator{\n    protected $ids;\n    protected $index;\n    protected $data = array();\n    public function __construct(){\n    $db = new MySQLis();\n    $db->connect('localhost', 'func', 'passwd', 'test');\n    $res = $db->query('select id from user');\n    $this->ids = $res->fetch_all(MYSQLI_ASSOC);\n    }\n    \u002F\u002F返回当前索引游标指向的元素 \n    public function  current(){\n    $id = $this->ids[$this->index]['id'];\n    return Factory::getUser($id);\n    }   \n    \u002F\u002F移动当前索引游标到下一元素  \n    public function next(){\n    $this->index ++;\n    }\n    \u002F\u002F返回当前索引游标指向的键  \n    public function key(){\n    return $this->index;\n    }\n    \u002F\u002F判断当前索引游标指向的元素是否有效 \n    public function valid(){\n    return $this->index \u003C count($this->ids);\n    }\n    \u002F\u002F重置索引游标  \n    public function rewind(){\n    $this->index = 0;\n    }\n}\n$user = new AllUser();\nforeach ($user as $key => $value) {\n   print_r($value);\n｝\n```\n二、代理模式\n\n在客户端与实体之间建立一个代理对象(proxy)，客户端对实体进行操作全部委派代理对象，隐藏实体的具体实现细节；\n\nproxy还可以与业务代码分离，部署到另外的服务器。业务代码中通过RPC来委派任务\n\n数据库主从，通过代理设置主从读写设置。\n\n示例代码：\n```php\nclass MySQLis{\n    protected $conn;\n    public function connect($host, $user, $pwd, $dbname){\n    $this->conn = mysqli_connect($host, $user, $pwd, $dbname);\n    }\n    public function query($sql){\n    $res =mysqli_query($this->conn, $sql);\n    return $res;\n    }\n    public function close(){\n    mysqli_close($this->conn);\n    }\n}\nclass Proxy implements IUserProxy{\n    public function getUserName($id){\n    $db = new MySQLis();\n    $db->connect('localhost', 'func', 'passwd', 'test');\n    $db->query('select * from user where id='.$id);\n    }\n    public function setUserName($id, $name){\n    $db = new MySQLis();\n    $db->connect('localhost', 'func', 'passwd', 'test');\n    $db->query(\"update user set name= '{$name}' where id= {$id}\");\n    }\n}\ninterface IUserProxy{\n    public function getUserName($id);\n    public function setUserName($id, $name);\n}\n$proxy = new Proxy();\n$proxy->getUserName(1);\n$proxy->setUserName(1, 'admin');\n```\n \n",null,654,0,"",1,"2019-01-17 23:03:13","2026-04-19 15:14:50",139,"PHP",{"id":23,"title":24},28,"PHP设计模式之观察者、原型以及装饰模式",{"id":26,"title":27},31,"整理Yii2中定义的常用路径别名",[29,31],{"id":30,"name":21},25,{"id":32,"name":33},47,"设计模式",2140,5,"https:\u002F\u002Ftp.myong.top\u002Fstorage\u002Farticle\u002F4d\u002F8611dcff9fff48cf5ed6c42328bb1e.jpg",[38,43,48,53,58],{"id":39,"title":40,"create_time":41,"description":42},93,"PHP执行Python脚本解压，压缩文件夹操作","2020-08-06 14:59:05","PHP是有解压缩类文件的，但在实际项目中，用户方使用Python脚本压缩，很容易出现中文解压乱码，导致文件内容丢失的情况，本文主要介绍PHP执行Python脚本解压缩出现的问题以及解决方法",{"id":44,"title":45,"create_time":46,"description":47},92,"PHP对图片的处理","2020-07-30 15:53:38","图片处理函数功能：缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路：当有图片有改动时自动生成一张新图片",{"id":49,"title":50,"create_time":51,"description":52},96,"PHP中字符串跟0做比较永远是true","2021-02-04 16:16:27","最近在项目中做数据对比的功能，发现一个特殊的情况，0跟任何字符串做比较永远返回true，特意了解了一下。",{"id":54,"title":55,"create_time":56,"description":57},13,"Win系统下配置PHP链接Oracle","2019-01-17 22:23:36","该文章内容主要介绍window系统下，php连接oracle的配置。",{"id":59,"title":60,"create_time":61,"description":62},103,"面向对象三大特性五大原则 + 低耦合高内聚[转载]","2021-10-31 23:18:58","面向对象的三大特性是\"封装、\"多态\"、\"继承\"，五大原则是\"单一职责原则\"、\"开放封闭原则\"、\"里氏替换原则\"、\"依赖倒置原则\"、\"接口分离原则\"。",1783431668820]