版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013862108/article/details/86635604
是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作
将复杂的SQL语句映射成对象属性的操作。
index.php
//数据对象映射模式
$user = new \Imooc\User(1);
var_dump($user->id, $user->mobile, $user->name, $user->regtime);
$user->mobile = '13146105129';
$user->name = 'lisi';
$user->regtime = date('Y-m-d H:i:s');
User.php
<?php
/**
* Created by PhpStorm.
* User: zhaozhiliang
* Date: 2019/1/11
* Time: 下午10:53
*/
namespace Imooc;
class User
{
public $id;
public $name;
public $mobile;
public $regtime;
public $serial_no;
protected $db;
function __construct($id)
{
$this->db = new \Imooc\Database\MySQLi();
$this->db->connect('192.168.199.129', 'work','123qwe', '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'];
$this->serial_no = $data['serial_no'];
}
function __destruct()
{
$ret = $this->db->query("update user set name='{$this->name}', mobile='{$this->mobile}', regtime='{$this->regtime}'
,serial_no='{$this->serial_no}' where id={$this->id}");
}
}