Forum Moderators: coopster

Message Too Old, No Replies

Newbie OOP architecture question

should I use objects within objects and if so, what syntax

         

formasfunction

12:30 am on Nov 12, 2007 (gmt 0)

10+ Year Member



I'm brand new to OOP and I have an architecture question. I have a class named Comment and a class named User. The user class is used a couple of different ways but one way I was going to use it was to hold the info of the author of a comment. My question is, is the best way to do this by creating a new User object in the constructor of my Comment class or some other way I'm not aware of? Here's what I have so far:

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

or would I utilize the User class' existing method like so:

$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.

FourDegreez

7:47 pm on Nov 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have _author set as private, so $comment->_author->getId(); wouldn't work; you'd need a getAuthor() method.

I would definitely go with the option of accessing the User object instead of duplicating all the User methods.