PHP數(shù)據(jù)對(duì)象映射模式實(shí)例分析
本文實(shí)例講述了PHP數(shù)據(jù)對(duì)象映射模式。分享給大家供大家參考,具體如下:
將對(duì)象和數(shù)據(jù)存儲(chǔ)映射起來,對(duì)一個(gè)對(duì)象的操作映射為對(duì)數(shù)據(jù)存儲(chǔ)的操作。
例如在代碼中new
一個(gè)對(duì)象,使用數(shù)組對(duì)象映射模式可以將對(duì)象的一些操作,比如設(shè)置一些屬性,就會(huì)自動(dòng)保存到數(shù)據(jù)庫,跟數(shù)據(jù)庫表的一條記錄對(duì)應(yīng)起來
在代碼中實(shí)現(xiàn)數(shù)據(jù)對(duì)象映射模式,我們將實(shí)現(xiàn)一個(gè)ORM類,將復(fù)雜的SQL語句映射成對(duì)象屬性的操作。同時(shí)結(jié)合工廠模式和注冊(cè)模式使用
例1
【例1】
數(shù)據(jù)庫 test ,user 表結(jié)構(gòu):
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) CHARACTER SET utf8 DEFAULT NULL, `mobile` varchar(11) CHARACTER SET utf8 DEFAULT NULL, `regtime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
Common\User.php:
<?php namespace Common; class User{ public $id; public $name; public $mobile; public $regtime; protected $db; //構(gòu)造方法 function __construct($id) { $this->db = new Database\MySQLi(); $conn = $this->db->connect('127.0.0.1', 'root', '', 'test'); $res = $this->db->query("select * from user where id = {$id} limit 1"); $data = $res->fetch_assoc(); $this->id = $data['id']; $this->name = $data['name']; $this->mobile = $data['mobile']; $this->regtime = $data['regtime']; } //析構(gòu)方法 function __destruct() { $this->db->query("update user set name = '{$this->name}', mobile = '{$this->mobile}', regtime = '{$this->regtime}' where id = {$this->id} limit 1"); } }
Common\Databases\MySQLi.php
<?php namespace Common\Database; use Common\IDatabase; class MySQLi implements IDatabase{ protected $conn; function connect($host, $user, $passwd, $dbname){ $conn = mysqli_connect($host, $user, $passwd ,$dbname); $this->conn = $conn; } function query($sql){ $res = mysqli_query($this->conn, $sql); return $res; } function close(){ mysqli_close($this->conn); } }
入口文件 index.php
<?php define('BASEDIR',__DIR__); //定義根目錄常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('\\Common\\Loader::autoload'); echo '<meta http-equiv="content-type" content="text/html;charset=utf8">'; /* * 對(duì)對(duì)象屬性的操作就完成了對(duì)數(shù)據(jù)庫的操作 */ $user = new Common\User(1); //讀取數(shù)據(jù) var_dump($user->id, $user->mobile, $user->name, $user->regtime);exit(); $user->mobile = '13800138000'; $user->name = 'Arshavin'; $user->regtime = date("Y-m-d H:i:s",time());
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP中數(shù)據(jù)庫單例模式的實(shí)現(xiàn)代碼分享
- php設(shè)計(jì)模式 DAO(數(shù)據(jù)訪問對(duì)象模式)
- 淺析php設(shè)計(jì)模式之?dāng)?shù)據(jù)對(duì)象映射模式
- PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫操作基類
- PHP的中使用非緩沖模式查詢數(shù)據(jù)庫的方法
- PHP實(shí)現(xiàn)的數(shù)據(jù)對(duì)象映射模式詳解
- PHP單例模式應(yīng)用示例【多次連接數(shù)據(jù)庫只實(shí)例化一次】
- PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化實(shí)現(xiàn)方法
- PHP設(shè)計(jì)模式之?dāng)?shù)據(jù)訪問對(duì)象模式(DAO)原理與用法實(shí)例分析
- PHP數(shù)據(jù)源架構(gòu)模式之表入口模式實(shí)例分析
相關(guān)文章
Php獲取金書網(wǎng)的書名的實(shí)現(xiàn)代碼
php獲取金書網(wǎng)的書名的實(shí)現(xiàn)代碼。2010-06-06PHP中執(zhí)行MYSQL事務(wù)解決數(shù)據(jù)寫入不完整等情況
事務(wù)可以進(jìn)行模擬SQL操作,當(dāng)所有的SQL都操作成功的時(shí)候才進(jìn)行SQL操作,只要有一個(gè)操作失敗就回滾當(dāng)前事務(wù)的所有SQL操作,避免出現(xiàn)上面描述中出現(xiàn)的數(shù)據(jù)寫入不完整等情況2014-01-01php實(shí)現(xiàn)parent調(diào)用父類的構(gòu)造方法與被覆寫的方法
這篇文章主要介紹了php實(shí)現(xiàn)parent調(diào)用父類的構(gòu)造方法與被覆寫的方法,在上一篇關(guān)于使用類繼承解決代碼重復(fù)問題的基礎(chǔ)上,進(jìn)一步分析了parent調(diào)用父類的構(gòu)造方法與被覆寫的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02