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

3 に答える 3

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'];
于 2013-02-24T07:06:39.893 に答える
2

user_data関数がデータの連想配列を返していると仮定すると、次のように使用してフィールドにアクセスできるはずです。

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

Lighthartの例のように、それらにアクセスするための関数を使用してプライベート変数を作成することをお勧めします。

もう1つのオプションは、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;
  }
}
于 2013-02-24T07:07:13.863 に答える
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つのパブリックget関数があります。getPasswordは、公開されたくないため、省略されました。また、新しい管理者を設定することも考えられるので、パブリックセッター機能も追加しました。したがって、新しいユーザーをインスタンス化します(つまり、クラスを取得して実際の例を作成します)。

$user1 = new User(1);

そして、使用中にこれらの変数を次のようにエコーします。

echo $user1->getUsername();

あなたの質問に直接答えなかったことをお詫びしますが、その例は問題に向かっています。

于 2013-02-24T06:49:02.773 に答える