Page 1 of 1

The output of an array of objects

Posted: Thu Feb 07, 2008 10:14 am
by devil78
Hi to all!

Code: Select all

 
class Corso {
 
    protected $_nome;
    protected $_tipologia;
    protected $_moduli = array();
 
    public function __construct($nome, $tipologia) {
        $this->_nome = $nome;
        $this->_tipologia = $tipologia;
    }
 
    public function setModuli(array $moduli) {
        foreach($moduli as $ii => $moduli) {
            if (!($moduli instanceof Modulo)) {
                throw new Exception("Elemento in indice $ii non è una valida istanza della classe modulo");
            }
            else{
              $this->_moduli[] = $moduli;
            }
        }
    }
 
    public function getModuli() {
        return $this->_moduli;
    }
}
 
As you can view in the code, $_moduli is an array of objects... How i can access to this data? Foreach doesn't work...

i did:

Code: Select all

 
$moduli = array(
  new Modulo('cip'),
  new Modulo('ciop'),
);
$corso = new Corso('nome', 'tipologia');
$corso->setModuli($moduli);
$corso->getModuli();
 


and now? thank you!

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 10:58 am
by Kieran Huggins
where's the code you've tried so far?

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 11:26 am
by devil78
This is my new try:

Code: Select all

 
public function print_moduli(){
      foreach ($this->_moduli as $moduli){
        echo $moduli."<br>";
      }
    }
 
$corso->print_moduli();
 
Catchable fatal error: Object of class Modulo could not be converted to string

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 12:58 pm
by Christopher
Usually it would be something like:

Code: Select all

 
public function print_moduli(){
      foreach ($this->_moduli as $moduli){
        echo $moduli->getName()."<br>";
      }
    }
 
$corso->print_moduli();
 

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 1:09 pm
by devil78
Ok, thank you!
:D :D :D

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 1:59 pm
by Kieran Huggins

Code: Select all

$this->docente = $docente;
should be

Code: Select all

$this->_docente = $docente;
then it should work

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 2:01 pm
by Christopher
Prego! ;)

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 3:53 pm
by Kieran Huggins
La penna è sul tavolo.

Dov'è il bagno?

....and that's the limit of my Italian :-(

Re: The output of an array of objects

Posted: Thu Feb 07, 2008 5:04 pm
by Christopher
I would like a table and a bathroom please...

There are eels in my hovercraft....