最后执行$user->serial_no=rand(1000,90000); 为什么能保存到数据库啊?
最后执行$user->serial_no=rand(1000,90000); 为什么能保存到数据库啊????这也是数据对象映射模式原理???还是这个属于迭代器模式的一个用处?因为我没看到你保存到数据库在哪里保存的啊
最后执行$user->serial_no=rand(1000,90000); 为什么能保存到数据库啊????这也是数据对象映射模式原理???还是这个属于迭代器模式的一个用处?因为我没看到你保存到数据库在哪里保存的啊
2016-09-16
user.php代码需要修改一下
<?php
namespace IMooc;
class User
{
//数据库表
public $id;
public $name;
public $mobile;
public $regtime;
public $seril_no;
protected $data;
protected $db;
function __construct($id)
{
/**
* 适配器模式-统一接口
* 工厂模式,在一个地方创建对象
* 注册树模式,同一个对象,只创建一次
*/
$this->db = \IMooc\Factory::createDBMySQLi();
$this->db->connect('localhost', 'root', '', 'test', 'utf8');
$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->seril_no = $data['seril_no'];
}
//析构方法
function __destruct()
{
$this->db->query("update user set name ='{$this->name}', mobile='{$this->mobile}',
regtime='{$this->regtime}', seril_no='{$this->seril_no}' where id = {$this->id} limit 1");
}
}举报