主頁 > 知識庫 > PHP數(shù)據(jù)對象映射模式實例分析

PHP數(shù)據(jù)對象映射模式實例分析

熱門標簽:地下城堡2圖九地圖標注 西區(qū)企業(yè)怎么做地圖標注入駐 九江外呼系統(tǒng) 抖音有個地圖標注是什么意思 保定crm外呼系統(tǒng)運營商 阿里云400電話申請加工單 七魚外呼系統(tǒng)停用嗎 智能電話機器人排名前十名南京 海南人工外呼系統(tǒng)有效果嗎

本文實例講述了PHP數(shù)據(jù)對象映射模式。分享給大家供大家參考,具體如下:

將對象和數(shù)據(jù)存儲映射起來,對一個對象的操作映射為對數(shù)據(jù)存儲的操作。

例如在代碼中new 一個對象,使用數(shù)組對象映射模式可以將對象的一些操作,比如設置一些屬性,就會自動保存到數(shù)據(jù)庫,跟數(shù)據(jù)庫表的一條記錄對應起來

在代碼中實現(xiàn)數(shù)據(jù)對象映射模式,我們將實現(xiàn)一個ORM類,將復雜的SQL語句映射成對象屬性的操作。同時結合工廠模式和注冊模式使用

例1

【例1】

數(shù)據(jù)庫 test ,user 表結構:

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;
  //構造方法
  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'];
  }
  //析構方法
  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">';
 /*
 * 對對象屬性的操作就完成了對數(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());

更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PHP中數(shù)據(jù)庫單例模式的實現(xiàn)代碼分享
  • php設計模式 DAO(數(shù)據(jù)訪問對象模式)
  • 淺析php設計模式之數(shù)據(jù)對象映射模式
  • PHP基于單例模式實現(xiàn)的數(shù)據(jù)庫操作基類
  • PHP的中使用非緩沖模式查詢數(shù)據(jù)庫的方法
  • PHP實現(xiàn)的數(shù)據(jù)對象映射模式詳解
  • PHP單例模式應用示例【多次連接數(shù)據(jù)庫只實例化一次】
  • PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化實現(xiàn)方法
  • PHP設計模式之數(shù)據(jù)訪問對象模式(DAO)原理與用法實例分析
  • PHP數(shù)據(jù)源架構模式之表入口模式實例分析

標簽:遼陽 韶關 涼山 十堰 九江 昭通 梅河口 甘肅

巨人網(wǎng)絡通訊聲明:本文標題《PHP數(shù)據(jù)對象映射模式實例分析》,本文關鍵詞  PHP,數(shù)據(jù),對象,映射,模式,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP數(shù)據(jù)對象映射模式實例分析》相關的同類信息!
  • 本頁收集關于PHP數(shù)據(jù)對象映射模式實例分析的相關信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章