<?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();