浅析php设计模式之数据对象映射模式,浅析设计模式
浅析php设计模式之数据对象映射模式,浅析设计模式
php中的设计模式中有很多的各种模式了,在这里我们来为各位介绍一个不常用的数据映射模式吧,希望文章能够帮助到各位。
数据映射模式使您能更好的组织你的应用程序与数据库进行交互。
数据映射模式将对象的属性与存储它们的表字段间的结合密度降低。数据映射模式的本质就是一个类,它映射或是翻译类的属性或是方法到数据库的相应字段,反之亦然。
数据映射的作用(工作)就在于能对双方所呈现出的信息的理解,并能对信息的存取进行控制,如根据存储在数据表中的信息
重建新的域对象,或是用域对象的信息来更新或删除数据表中的相关数据。
对于面向对象代码与数据库表和字段间的映射关系的存储有多种实现方式。其中一种可能的方法就通过手工编码将这种映射关系存储在数据映射类中。
另一种可选的方法是用PHP的数组并将其编码为类本身。这个类也能外源获取数据,如INI或是XML文件。
数据对象映射模式,是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作。
在代码中实现数据对象映射模式,实现一个ORM类,将复杂的sql语句映射成对象属性的操作。对象关系映射(Object Relational Mapping,ORM)
ha_cl表
Hacl.php
<?php namespace Baobab; class Hacl{ public $id; public $haclname; public $haclcode; public $hacls; protected $db; function __construct($id){ $this->db = new \Baobab\Database\Mysqli(); $this->db->connect('127.0.0.1', 'root', '', 'test'); $res = $this->db->query("select * from ha_cl where id = {$id}"); $data = $res->fetch_assoc(); $this->id = $data['ID']; $this->haclname = $data['ha_cl_name']; $this->haclcode = $data['ha_cl_code']; $this->hacls = $data['hacls']; } function __destruct(){ $this->db->query("update ha_cl set ha_cl_code = '{$this->haclcode}', ha_cl_name = '{$this->haclname}', hacls = '{$this->hacls}' where ID = {$this->id} limit 1"); } }
Factory.php
<?php namespace Baobab; class Factory{ static function getHacl($id){ $key = 'user_'.$id; $user = \Baobab\Register::get($key);//表中id不同表示的是不同的对象 if(!$user){ $user = new \Baobab\Hacl($id); \Baobab\Register::set($key, $user); } return $user; } }
Register.php
<?php namespace Baobab; class Register{ protected static $objects; static function set($alias, $object){ self::$objects[$alias] = $object; } static function _unset($alias) { unset(self::$objects[$alias]); } static function get($name) { return self::$objects[$name]; } }
index.php
class Page{ function index(){ $hacl = Baobab\Factory::getHacl(13); $hacl->haclname = '测试名称'; $this->test(); echo 'ok'; } function test(){ $hacl = Baobab\Factory::getHacl(13); $hacl->hacls = '测试内容'; } } $page = new Page(); $page->index();
使用工厂模式会多次创建对象Hacl,浪费资源,如果将对象作为参数传递,一方面会带来额外的使用成本,另外如果很多地方都用到这个对象很容易发生错误,因此在工厂模式中使用注册树模式来解决这个问题。
以上内容给大家介绍了php设计模式之数据对象映射模式,希望对大家有所帮助!
您可能感兴趣的文章:
- PHP 5 数据对象 (PDO) 抽象层与 Oracle
- 回答PHPCHINA上的几个问题:URL映射
- 解密ThinkPHP3.1.2版本之模块和操作映射
- ThinkPHP中公共函数路径和配置项路径的映射分析
- 详解PHP的Laravel框架中Eloquent对象关系映射使用

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

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

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



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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
