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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools