search
Homephp教程php手册php设计模式 数据对象映射模式,php设计模式

php设计模式 数据对象映射模式,php设计模式

数据对象映射模式,是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作。

在代码中实现数据对象映射模式,实现一个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,浪费资源,如果将对象作为参数传递,一方面会带来额外的使用成本,另外如果很多地方都用到这个对象很容易发生错误,因此在工厂模式中使用注册树模式来解决这个问题。

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.