0

I'm new to OOP and very confused about it. A class that collected user info from database based on the ID passed though:

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);

How do I get all the variables in the array? How do I echo out the username for example?

3 Answers 3

4

Simply try this.

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'];
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming your user_data function is returning an associative array of data, you should be able to access the fields using as such:

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

As in Lighthart's example, it would be good practice to create private variables, with functions to access them.

Another option would be to implement the ArrayAccess interface (http://php.net/manual/en/class.arrayaccess.php) Using this interface, you would be able to use your object similarly to how you use an array. That is, you could use:

echo $user['username'];

As a starting point, you could try something like:

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;
  }
}

Comments

0

The example you have given probably won't work for what you are trying to accomplish. It is not clear what function userdata does. Revised:

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; }

}

The variables are set private. Only the user object should have access to the data directly. However, other objects might want to retrieve the data, which is why there are 4 public get functions. getPassword was omitted because you probably don't want that one publicly available. Also, it is concievable you might set a new admin, so a public setter function was added as well. you would instance the new user (that is, take the class and make a real example of it) thusly:

$user1 = new User(1);

And during usage you would echo these variables by:

echo $user1->getUsername();

Please accept my apologies for not directly answering your question, but that example is headed for trouble.

2 Comments

The user_data function fetches data from the database. How would I incorporate where those variables go?
Editted based on my best guess of the code. You'll have to post the user_data function if my edits are not sufficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.