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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
