2013-02-24 55 views
0

我是OOP的新手,对此很困惑。根据传递的ID通过数据库收集用户信息的类:如何检索类中的数组变量

class user { 

    public $profile_data; 
    public function __construct($profile_user_id) { 
     $this->profile_data = user_data($profile_user_id, 'id', 'username', 'password', 'email', 'admin'); 
    } 

} 

$profile_data[] = new user(1); 

如何获取数组中的所有变量?例如,我如何回应username

回答

4

只需试试这个。

class user { 

     public $profile_data; 
     public function __construct($profile_user_id) { 
      $this->profile_data = user_data($profile_user_id, 'id', 'username', 'password', 'email', 'admin'); 
     } 

    } 

    $userObj = new user(1); 
    $profileData = $userObj->profile_data; 
    echo $profileData['username']; 
0

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

请接受我对于不直接回答你的问题表示歉意,但是这个例子会带来麻烦。

+0

的'user_data'功能从数据库中获取数据。我将如何纳入这些变量的去向? – Benny 2013-02-24 06:54:25

+0

user_date的返回格式是什么? – Lighthart 2013-02-24 06:57:25

+0

它返回一个数组。 – Benny 2013-02-24 06:58:08

2

假设你USER_DATA函数返回数据的关联数组,你应该能够利用这样来访问字段:

$profile = new user(1); 
echo $profile->profile_data['username']; 

正如Lighthart的例子,这将是很好的做法,创建私有变量,具有访问它们的函数。

另一个选择是实现ArrayAccess接口(http://php.net/manual/en/class.arrayaccess.php)使用这个接口,您可以像使用数组一样使用对象。也就是说,你可以使用:

echo $user['username']; 

作为一个起点,你可以尝试这样的:

class user implements ArrayAccess { 
    private $data; 
    public function __construct($profile_user_id) { 
    $this->data= user_data($profile_user_id, 'id', 'username', 'password', 'email', 'admin');  
    } 

    public function offsetSet($offset, $value) { 
    // Do nothing, assuming non mutable data - or throw an exception if you want 
    } 

    public function offsetExists($offset) { 
    return isset($this->data[$offset]); 
    } 

    public function offsetUnset($offset) { 
    // Do nothing, assuming non mutable data - or throw an exception if you want 
    } 

    public function offsetGet($offset) { 
    return isset($this->data[$offset]) ? $this->data[$offset] : null; 
    } 
}