亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺析php設(shè)計(jì)模式之?dāng)?shù)據(jù)對(duì)象映射模式

 更新時(shí)間:2016年03月03日 10:32:22   作者:tianxintian22  
php中的設(shè)計(jì)模式中有很多的各種模式了,在這里我們來(lái)為各位介紹一個(gè)不常用的數(shù)據(jù)映射模式吧,感興趣的朋友一起看下吧

php中的設(shè)計(jì)模式中有很多的各種模式了,在這里我們來(lái)為各位介紹一個(gè)不常用的數(shù)據(jù)映射模式吧,希望文章能夠幫助到各位。

數(shù)據(jù)映射模式使您能更好的組織你的應(yīng)用程序與數(shù)據(jù)庫(kù)進(jìn)行交互。

數(shù)據(jù)映射模式將對(duì)象的屬性與存儲(chǔ)它們的表字段間的結(jié)合密度降低。數(shù)據(jù)映射模式的本質(zhì)就是一個(gè)類,它映射或是翻譯類的屬性或是方法到數(shù)據(jù)庫(kù)的相應(yīng)字段,反之亦然。

數(shù)據(jù)映射的作用(工作)就在于能對(duì)雙方所呈現(xiàn)出的信息的理解,并能對(duì)信息的存取進(jìn)行控制,如根據(jù)存儲(chǔ)在數(shù)據(jù)表中的信息

重建新的域?qū)ο?,或是用域?qū)ο蟮男畔?lái)更新或刪除數(shù)據(jù)表中的相關(guān)數(shù)據(jù)。

對(duì)于面向?qū)ο蟠a與數(shù)據(jù)庫(kù)表和字段間的映射關(guān)系的存儲(chǔ)有多種實(shí)現(xiàn)方式。其中一種可能的方法就通過(guò)手工編碼將這種映射關(guān)系存儲(chǔ)在數(shù)據(jù)映射類中。

另一種可選的方法是用PHP的數(shù)組并將其編碼為類本身。這個(gè)類也能外源獲取數(shù)據(jù),如INI或是XML文件。

數(shù)據(jù)對(duì)象映射模式,是將對(duì)象和數(shù)據(jù)存儲(chǔ)映射起來(lái),對(duì)一個(gè)對(duì)象的操作會(huì)映射為對(duì)數(shù)據(jù)存儲(chǔ)的操作。

在代碼中實(shí)現(xiàn)數(shù)據(jù)對(duì)象映射模式,實(shí)現(xiàn)一個(gè)ORM類,將復(fù)雜的sql語(yǔ)句映射成對(duì)象屬性的操作。對(duì)象關(guān)系映射(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不同表示的是不同的對(duì)象
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 = '測(cè)試名稱';
$this->test();
echo 'ok';
}
function test(){
$hacl = Baobab\Factory::getHacl(13);
$hacl->hacls = '測(cè)試內(nèi)容';
}
}
$page = new Page();
$page->index(); 

使用工廠模式會(huì)多次創(chuàng)建對(duì)象Hacl,浪費(fèi)資源,如果將對(duì)象作為參數(shù)傳遞,一方面會(huì)帶來(lái)額外的使用成本,另外如果很多地方都用到這個(gè)對(duì)象很容易發(fā)生錯(cuò)誤,因此在工廠模式中使用注冊(cè)樹(shù)模式來(lái)解決這個(gè)問(wèn)題。

以上內(nèi)容給大家介紹了php設(shè)計(jì)模式之?dāng)?shù)據(jù)對(duì)象映射模式,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論