您给出的例子可能不适用于您要完成的任务。目前还不清楚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