Home php教程 php手册 浅析php设计模式之数据对象映射模式

浅析php设计模式之数据对象映射模式

Jun 06, 2016 pm 07:33 PM
php object data mapping model Design Patterns

php中的设计模式中有很多的各种模式了,在这里我们来为各位介绍一个不常用的数据映射模式吧,希望文章能够帮助到各位。 数据映射模式使您能更好的组织你的应用程序与数据库进行交互。 数据映射模式将对象的属性与存储它们的表字段间的结合密度降低。数据映射

php中的设计模式中有很多的各种模式了,在这里我们来为各位介绍一个不常用的数据映射模式吧,希望文章能够帮助到各位。

数据映射模式使您能更好的组织你的应用程序与数据库进行交互。

数据映射模式将对象的属性与存储它们的表字段间的结合密度降低。数据映射模式的本质就是一个类,它映射或是翻译类的属性或是方法到数据库的相应字段,反之亦然。

数据映射的作用(工作)就在于能对双方所呈现出的信息的理解,并能对信息的存取进行控制,如根据存储在数据表中的信息

重建新的域对象,或是用域对象的信息来更新或删除数据表中的相关数据。

对于面向对象代码与数据库表和字段间的映射关系的存储有多种实现方式。其中一种可能的方法就通过手工编码将这种映射关系存储在数据映射类中。

另一种可选的方法是用PHP的数组并将其编码为类本身。这个类也能外源获取数据,如INI或是XML文件。

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

在代码中实现数据对象映射模式,实现一个ORM类,将复杂的sql语句映射成对象属性的操作。对象关系映射(Object Relational Mapping,ORM)

ha_cl表

Hacl.php

<&#63;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");
}
}
Copy after login

Factory.php

<&#63;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;
}
}
Copy after login

Register.php

<&#63;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];
}
}
Copy after login

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(); 
Copy after login

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

以上内容给大家介绍了php设计模式之数据对象映射模式,希望对大家有所帮助!

Statement of this Website
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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles