Clase Servicio
Código PHP:
class Service{
private $_id;
...
private $_userComments = array();
public function __toString() {
$s .= $this->getId();
$s .= ' | ';
foreach ($this->getUserComments() as $num => $c) {
$s .= $num . '. ' . $c->getComment() . '<br/>';
}
return $s;
}
public function addComment(UserComment $comment){
array_push($this->getUserComments(), $comment);
}
Código PHP:
class UserComment{
private $_id;
...
private $_comment;
public function getComment() { return $this->_comment; }
public function setComment($comment) { $this->_comment = $comment; }
...
...
}
Código PHP:
require_once('config.php');
require_once(CLASSES.'/Service.php');
require_once(CLASSES.'/UserComment.php');
$s = new Service();
$c = new UserComment();
$c->setComment('Este es un comentario de un usuario');
$d = new UserComment();
$d->setComment('Este OTRO comentario de un usuario');
$s->addComment($c);
$s->addComment($d);
print_r($s->getUserComments());
echo $s;