php设计模式-数据对象映射

<?php

class User 
{
    public $id;
    public $name;
    public $age;

    private $_db;

    public function __construct($id)
    {
        $this->_db  = new MySQLi();
        $conn       = $this->db->connect('127.0.0.1', 'root', '', 'test');
        $result     = $this->db->query("select * from user where id = {$id} limit 1");
        $data       = $result->fetch_assoc();

        $this->id   = $id;
        $this->name = $data['name'];
        $this->age  = $data['age'];
    }   

    public function save()
    {
        $this->_db->query("update user set name = '{$this->name}', age = '{$this->age}'");
    }
}

$user = new User(1);
$user->username = 'zhansan';
$user->age = 12;
$user->save();

  

posted @ 2020-07-21 22:21  coder_xds  阅读(109)  评论(0编辑  收藏  举报