Using a returned objects array from a PHP function -
i'm having weird problem. i'm learning php (used program in java), , i'm trying 1 simple thing. have dao method this:
while (oci_fetch($parse)) { $stats = new stats(); $stats->setid($id); $stats->setname($name); $stats->setemail($email); $stats->setgender($gender); $stats->setbirthday($birthday); $statslist[] = $stats; } return $statslist;
and have php file uses function (called getbyid), test, this:
$statsdao = new statsdao(); $statslist[] = $statsdao->getbyid(1); foreach ($statslist $stat) { echo $stat->getname(); }
this seems simple enough: dao returns array , other file reads returned array of stats , prints it. i'm getting error message instead:
fatal error: call member function getname() on non-object in /var/www/socializi/interfaceusuario/index.php on line 25
the weird thing is: if call foreach loop inside dao's getbyid function, prints alright, when call function outside , assign array, not recognize array's contents objects.
what doing wrong here? thanks!
try that:
$statsdao = new statsdao(); $statslist = $statsdao->getbyid(1); foreach ($statslist $stat) { echo $stat->getname(); }
you add [] served nothing more put array inside array giving you
$statslist[0][0]->getname(); //would work on php 5.4 syntaxe shoes idea.
one level deep.
put foreach inside first 1 fix ugly , serves nothing:
$statsdao[] = new statsdao(); $statslist = $statsdao->getbyid(1); foreach ($statslist $stats) { foreach ($stats $stat) { echo $stat->getname(); } }
Comments
Post a Comment