I’m having a weird problem. I’m learning PHP (used to program in Java), and I’m trying to do one simple thing. I have a DAO method that does something like 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 I have another PHP file that uses this function (called getById), as a test, like this:
$statsdao = new StatsDAO();
$statslist[] = $statsdao->getById(1);
foreach ($statslist as $stat) {
echo $stat->getName();
}
This seems simple enough: the DAO returns an array and my other file reads the returned array of Stats and prints it. But i’m getting this error message instead:
Fatal error: Call to a member function getName() on a non-object in
/var/www/socializi/interfaceusuario/index.php on line 25
The weird thing is: if I call the foreach loop inside the DAO’s getById function, it prints it alright, but when I call the function from outside and assign it to another array, it does not recognize the array’s contents as objects.
What am I doing wrong here?
Thanks!
Try it like that:
You add a [] that served nothing more then put the array inside another array thus giving you
One level too deep.
Put another foreach inside the first one would also fix it but its ugly and serves nothing: