您給出的例子可能不適用於您要完成的任務。目前還不清楚userdata的功能是什麼。修訂:
class User {
private $id;
private $username;
private $password;
private $email;
private $admin;
public function __construct($id, $username, $password, $email, $admin) {
$profileData = user_data($profile_user_id
,'id'
,'username'
,'password'
,'email'
,'admin');
$this->id = $profileData ['id'];
$this->username = $profileData ['username'];
$this->password = $profileData ['password'];
$this->email = $profileData ['email'];
$this->admin = $profileData ['admin'];
}
public function getId(){ return $this->id; }
public function getUsername(){ return $this->username; }
public function getEmail(){ return $this->email; }
public function getAdmin(){ return $this->admin; }
public function setAdmin($admin){ $this->admin = $admin; }
}
變量設置爲私有。只有用戶對象才能直接訪問數據。但是,其他對象可能需要檢索數據,這就是爲什麼有4個公共獲取函數的原因。 getPassword被省略了,因爲你可能不希望公開提供那個。此外,它可以設置一個新的管理員,因此公共設置功能也被添加。你會實例的新用戶(即走班和做它的一個真實的例子)正是如此:
和使用過程中,你會附和這些變量是:
echo $user1->getUsername();
請接受我對於不直接回答你的問題表示歉意,但是這個例子會帶來麻煩。
的'user_data'功能從數據庫中獲取數據。我將如何納入這些變量的去向? – Benny 2013-02-24 06:54:25
user_date的返回格式是什麼? – Lighthart 2013-02-24 06:57:25
它返回一個數組。 – Benny 2013-02-24 06:58:08