Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/11/2008, 08:20
Avatar de chitoso
chitoso
 
Fecha de Ingreso: mayo-2008
Ubicación: Montevideo, Uruguay
Mensajes: 205
Antigüedad: 16 años
Puntos: 5
Problemita con array de objetos

Tengo la siguiente situación. La clase servicio tiene una lista de elementos UserComment. Agrego comments a un servicio y lo imprimo. La lista se encuentra vacía... No se porqué. Capaz que es como manipulo los array.

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);
    } 
Clase UserComment
Código PHP:
class UserComment{

    private 
$_id;
    ...
    private 
$_comment;

    public function 
getComment() { return $this->_comment; }
    public function 
setComment($comment) { $this->_comment $comment; }
    ...
    ...


Mi test
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
Estoy bloqueado...
__________________
Pasión por la Música