Forum Moderators: coopster
class Comment{
private $_id;
private $_parent_id;
private $_author;
private $_date;
private $_age;
private $_comment;
//
public function __construct($attributes){
//set initial variables
$this->_id = $attributes['id'];
$this->_parent_id = $attributes['parent_id'];
$this->_url_id = $attributes['url_id'];
$this->_date = $attributes['date'];
$this->_age = $attributes['age'];
$this->_comment = $attributes['comment'];
//
//feed author info into user object
$author_attributes = array(
'id'=> $attributes['author_id'],
'username'=> $attributes['author_username'],
'real_name'=> $attributes['author_realname'],
'email'=> $attributes['author_email']
);
$this->_author = new User( $author_attributes );
}
public function getId(){
return $this->_id;
}
//
public function getParentId(){
return $this->_parent_id;
}
//etc and so on
}
//
class User {
private $_id;
private $_username;
private $_real_name;
private $_icon;
private $_email;
private $_email_alias;
//
public function __construct($attributes){
$this->_id = $attributes['id'];
$this->_username = $attributes['username'];
$this->_real_name = $attributes['real_name'];
$this->_icon = "userfiles/images/thumbs/". $this->author_id .".jpg";
$this->_email = $attributes['email'];
}
public function getId(){
return $this->_id;
}
//
public function getUserName(){
return $this->_username;
}
//etc and so on
}
If this is a good way to do it, is it then proper to create methods in the Comment class to access variables in the User class or do you access User methods directly? For example - if I wanted to get the user id of the author associated with an instance of Comment named $comment would I create a new method and write:
$comment->getAuthorId();
$comment->_author->getId();
Being new at this, I haven't seen many objects within objects references in the newbie tutorials I've been reading so I don't know the best way to approach it. Thanks for the help and any recommendations for tutorials or books would be greatly appreciated.