Thyago B. Rodrigues
Thyago B. Rodrigues

Reputation: 630

Using a returned objects array from a PHP function

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!

Upvotes: 2

Views: 3854

Answers (3)

mikaelb
mikaelb

Reputation: 2148

The reson is that you are using $statslist[] = $statsdao->getById(1);, which creates the results inside the first element of a $statslist array. So drop [].

You can activate error_reporting(E_ALL); to avoid such issues. That will tell you that you are trying to append an element of a non-array variable.

Edit: I wrongfully remembered PHP giving this notice, if the array didn't exist. But it seems that the square bracket notation, $array[], creates an array $array, if it does not exist.

The error_reporting(E_ALL); is still a good idea in development enviroment, though. Helps you avoid sources of error.

Upvotes: 0

Iznogood
Iznogood

Reputation: 12843

Try it like that:

  $statsdao = new StatsDAO();
    $statslist = $statsdao->getById(1);
    foreach ($statslist as $stat) {
        echo $stat->getName();
    }

You add a [] that served nothing more then put the array inside another array thus giving you

 $statslist[0][0]->getName(); //would only work on php 5.4 with this syntaxe but it shoes the idea.

One level too deep.

Put another foreach inside the first one would also fix it but its ugly and serves nothing:

 $statsdao[] = new StatsDAO();
    $statslist = $statsdao->getById(1);
    foreach ($statslist as $stats) {
         foreach ($stats as $stat) {
             echo $stat->getName();
         }
    }

Upvotes: 3

Crashspeeder
Crashspeeder

Reputation: 4311

You don't need the brackets. when setting $statslist. By putting the brackets in you're telling PHP that you want to create a new array and set the first key (0) to the return value of getById(). That results in an array of arrays. You don't actually want that.

$statsdao = new StatsDAO();
$statslist = $statsdao->getById(1);
foreach ($statslist as $stat) {
    echo $stat->getName();
}

Upvotes: 0

Related Questions