将数据和对象存储起来,对一个对象的操作会映射为对数据存储的操作
实际应用:数据表user,对数据的操作。
核心代码
- user数据表类文件
<?php
namespace TT;
class User
{
public $id;
public $name;
public $phone;
protected $db;
function __construct($id)
{
$this->db = new \TT\Database\MySQLi();
$this->db->connect('127.0.0.1', 'root', '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->phone = $data['phone'];
}
function __destruct()
{
$this->db->query("update user set name='{$this->name}', phone='{$this->phone}' where id={$this->id}");
}
}
- 实现
<?php
$user = new TT\User(1);
$user->phone = '15998776577';
$uesr->name = '张三';