<?php
define("BASDIR",__DIR__);
include BASDIR."/Phpclass/Loader.php";
spl_autoload_register("\\Phpclass\\Loader::autoload_rege");
$user = new \Phpclass\User(1);
var_dump($user->id,$user->username,$user->phone_no,$user->retime);
$user->username = "nfyx1";//這里相當於 給user類中的屬性賦值
$user->phone_no = "132451111111";
$user->retime = date("Y-m-d H-i-s");
//最后,在程序執行完時,調用__destruct()函數,把函數里的代碼調協成update sql 語句,這時改變當前值,就等於更新一條數據。
-----------------------------------------------------------------------------------Index4.php
<?php
//對應的映射到數據庫中的user表
namespace Phpclass;
class User
{
public $id;
public $username;
public $phone_no;
public $retime;
protected $db;
function __construct($id)
{
$this->db= new Databases\Mysqlis();//創建一個Mysqlis對象,並把他保存到$db中;這里依賴於Databases目錄下的Mysqlis.php文件
$this->db->connect("127.0.0.1","root","root","nfyx");//連接
$res=$this->db->query("select * from user where id=$id");//查詢
$data=$res->fetch_assoc();//把結果集遍歷成一個數組$data;
$this->id=$data['id'];//把數組中的值 賦值 給當前對應的變量;
$this->username=$data['username'];
$this->phone_no=$data['phone_no'];
$this->retime=$data['retime'];
}
function __destruct()
{
$this->db->query("update user set username='{$this->username}',phone_no='{$this->phone_no}',retime='{$this->retime}' where id='{$this->id}'");
}
}
-----------------------------------------------------------------------------------User.php,對應的數據庫里的表