2013-02-24 53 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; 
    } 
}