数据对象映射模式,是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作。
在代码中实现数据对象映射模式,实现一个ORM类,将复杂的sql语句映射成对象属性的操作。对象关系映射(Object Relational Mapping,ORM)
ha_cl表
Hacl.php
<?<span>php namespace Baobab; </span><span>class</span><span> Hacl{ </span><span>public</span> <span>$id</span><span>; </span><span>public</span> <span>$haclname</span><span>; </span><span>public</span> <span>$haclcode</span><span>; </span><span>public</span> <span>$hacls</span><span>; </span><span>protected</span> <span>$db</span><span>;<br /></span> <span>function</span> __construct(<span>$id</span><span>){ </span><span>$this</span>->db = <span>new</span><span> \Baobab\Database\Mysqli(); </span><span>$this</span>->db->connect('127.0.0.1', 'root', '', 'test'<span>); </span><span>$res</span> = <span>$this</span>->db->query("select * from ha_cl where id = {<span>$id</span>}"<span>); </span><span>$data</span> = <span>$res</span>-><span>fetch_assoc(); </span><span>$this</span>->id = <span>$data</span>['ID'<span>]; </span><span>$this</span>->haclname = <span>$data</span>['ha_cl_name'<span>]; </span><span>$this</span>->haclcode = <span>$data</span>['ha_cl_code'<span>]; </span><span>$this</span>->hacls = <span>$data</span>['hacls'<span>]; } </span><span>function</span><span> __destruct(){ </span><span>$this</span>->db->query("<span>update ha_cl set ha_cl_code = '{</span><span>$this</span><span>->haclcode}', ha_cl_name = '{</span><span>$this</span><span>->haclname}', hacls = '{</span><span>$this</span><span>->hacls}' where ID = {</span><span>$this</span><span>->id} limit 1</span>"<span>); } }</span>
Factory.php
<?<span>php namespace Baobab; </span><span>class</span><span> Factory{ </span><span>static</span> <span>function</span> getHacl(<span>$id</span><span>){ </span><span>$key</span> = 'user_'.<span>$id</span><span>; </span><span>$user</span> = \Baobab\Register::get(<span>$key</span><span>);//表中id不同表示的是不同的对象 </span><span>if</span>(!<span>$user</span><span>){ </span><span>$user</span> = <span>new</span> \Baobab\Hacl(<span>$id</span><span>); \Baobab\Register</span>::set(<span>$key</span>, <span>$user</span><span>); } </span><span>return</span> <span>$user</span><span>; } }</span>
Register.php
<?<span>php namespace Baobab; </span><span>class</span><span> Register{ </span><span>protected</span> <span>static</span> <span>$objects</span><span>; </span><span>static</span> <span>function</span> set(<span>$alias</span>, <span>$object</span><span>){ self</span>::<span>$objects</span>[<span>$alias</span>] = <span>$object</span><span>; } </span><span>static</span> <span>function</span> _unset(<span>$alias</span><span>) { </span><span>unset</span>(self::<span>$objects</span>[<span>$alias</span><span>]); } </span><span>static</span> <span>function</span> get(<span>$name</span><span>) { </span><span>return</span> self::<span>$objects</span>[<span>$name</span><span>]; } }</span>
index.php
<span>class</span><span> Page{ </span><span>function</span><span> index(){ </span><span>$hacl</span> = Baobab\Factory::getHacl(13<span>); </span><span>$hacl</span>->haclname = '测试名称'<span>; </span><span>$this</span>-><span>test(); </span><span>echo</span> 'ok'<span>; } </span><span>function</span><span> test(){ </span><span>$hacl</span> = Baobab\Factory::getHacl(13<span>); </span><span>$hacl</span>->hacls = '测试内容'<span>; } } </span><span>$page</span> = <span>new</span><span> Page(); </span><span>$page</span>->index();
使用工厂模式会多次创建对象Hacl,浪费资源,如果将对象作为参数传递,一方面会带来额外的使用成本,另外如果很多地方都用到这个对象很容易发生错误,因此在工厂模式中使用注册树模式来解决这个问题。