主頁 > 知識庫 > PHP實現(xiàn)的數(shù)據(jù)對象映射模式詳解

PHP實現(xiàn)的數(shù)據(jù)對象映射模式詳解

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

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

還是代碼說話:這里還是遵循策略模式的psr-0代碼規(guī)范

數(shù)據(jù)表:

數(shù)據(jù)庫連接文件Db.php(如果沒有可以到前面一篇《PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化》里面找)
自動加載類文件Config.php(如果沒有可以去上一篇《PHP策略模式》里拿過來)

入口文件DataUser.php

?php
define('BASEDIR', __DIR__);
//自動加載在本文件中沒有被定義的類
require 'Config.php';
spl_autoload_register('Config::autolad');
//獲取數(shù)據(jù)
$user = new Data(1);
var_dump($user->id, $user->name, $user->money);
//如果想要修改數(shù)據(jù)
$user->id = 1;
$user->name = 'zhangjianping';
$user->money = 10000;
?>

獲取數(shù)據(jù)的文件Data.php

?php
class Data
{
//數(shù)據(jù)項
public $id;
public $name;
public $money;
//數(shù)據(jù)庫連接對象
protected $con;
//查詢數(shù)據(jù)的構(gòu)造函數(shù)
public function __construct($id)
{
  //連接數(shù)據(jù)庫
  $this->con = DB::getInstance()->connect();
  //查詢數(shù)據(jù)
  $res = $this->con->query('select * from account where id = '.$id.' limit 1');
  $data = $res->fetch(PDO::FETCH_ASSOC);
  //把取出來的數(shù)據(jù)項存儲起來
  $this->id = $data['id'];
  $this->name = $data['name'];
  $this->money = $data['money'];
}
//修改數(shù)據(jù)的析構(gòu)函數(shù)
public function __destruct()
{
  $this->con->query("update account set name = '{$this->name}', 'money = {$this->money}' where id = {$this->id}");
}
}
?>

下面我們就使用工廠模式,注冊樹模式,數(shù)據(jù)對象映射模式來完善一下這個例子

  • 數(shù)據(jù)庫連接文件Db.php
  • 自動加載類文件Config.php
  • 獲取數(shù)據(jù)的文件Data.php

我們將原來的入口文件改一下:

DataUser.php

?php
define('BASEDIR', __DIR__);
require 'Config.php';
spl_autoload_register(Config::autoload);
class DataUser
{
  public function index()
  {
    //使用工廠模式來生成對象
    $user = Factory::getUser(1);
    var_dump($user->id);
    $this->name();
    $this->money();
  }
  public function name()
  {
    $user = Factory::getUser(1);
    var_dump($user->name);
  }
  public function money()
  {
    $user = Factory::getUser(1);
    var_dump($user->money);
  }
}
?>

工廠類Factory.php

?php
class Factory
{
  static function getUser($id)
  {
    //這里使用注冊器模式,不然的話,在上面的文件中,使用工廠模式生成對象得時候就會多次創(chuàng)建對象,很占用資源
    //根據(jù)id不同插入到注冊樹對象中
    $key = 'user_'.$id;
    //從注冊器中取出對象
    $user = Register::get($key);
    //如果注冊器中沒有就創(chuàng)建一個對象并注冊上去
    if(!isset($user))
    {
      $user = new Data($id);
      $user = Register::set($key, $user);
    }
    return $user;
  }
}
?>

注冊器類Register.php

?php
class Register
{
  //存儲對象得變量
  protected static $object;
  //注冊入注冊器
  public static function set($key, $value)
  {
    self::$object[$key] = $value;
  }
  //從注冊器中取出
  public static function get($key)
  {
    return self::$object[$key];
  }
  //從注冊器中刪除
  public static function _unset($key)
  {
    unset(self::$object[$key]);
  }
}
?>

如果這時候我們將Data.php修改為Data1.php,那么在不使用工廠模式的時候就要一個一個的去修改類名,現(xiàn)在只需要在工廠模式中修改一下就好了,我們也可以打印出每一個對象,這時候我們會發(fā)現(xiàn)這3個對象都是一樣的,這是因為我們使用了注冊器模式。

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

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

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

標(biāo)簽:昭通 九江 十堰 涼山 遼陽 韶關(guān) 甘肅 梅河口

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