The output of an array of objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devil78
Forum Newbie
Posts: 7
Joined: Wed Aug 22, 2007 1:08 pm

The output of an array of objects

Post 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!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: The output of an array of objects

Post by Kieran Huggins »

where's the code you've tried so far?
devil78
Forum Newbie
Posts: 7
Joined: Wed Aug 22, 2007 1:08 pm

Re: The output of an array of objects

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: The output of an array of objects

Post 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();
 
(#10850)
devil78
Forum Newbie
Posts: 7
Joined: Wed Aug 22, 2007 1:08 pm

Re: The output of an array of objects

Post by devil78 »

Ok, thank you!
:D :D :D
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: The output of an array of objects

Post by Kieran Huggins »

Code: Select all

$this->docente = $docente;
should be

Code: Select all

$this->_docente = $docente;
then it should work
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: The output of an array of objects

Post by Christopher »

Prego! ;)
(#10850)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: The output of an array of objects

Post by Kieran Huggins »

La penna è sul tavolo.

Dov'è il bagno?

....and that's the limit of my Italian :-(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: The output of an array of objects

Post by Christopher »

I would like a table and a bathroom please...

There are eels in my hovercraft....
(#10850)
Post Reply