Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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();
         }
    }
share|improve this answer
    
The nested foreachs worked perfectly, thank you very much! Would you happen to know why this happens? I mean, if I return an array, I don't understand why I get an array inside an array... –  Thyago B. Rodrigues Mar 9 '12 at 18:03

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();
}
share|improve this answer

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.

share|improve this answer
    
That second part is wrong. He has error displayed he got a error its in his post! Also he is not appending anything he is calling a method of a non object :) –  Iznogood Mar 9 '12 at 17:37
    
@Iznogood He would've gotten a notice or warning stating that $statslist is not an array. –  Crashspeeder Mar 9 '12 at 17:42
    
@Crashspeeder It is a array! A one element array! –  Iznogood Mar 9 '12 at 17:43
1  
@Crashspeeder you are wrong friend try it out on writecodeonline.com/php it takes only a minute. :) –  Iznogood Mar 9 '12 at 17:50
1  
When using the square bracket notation, you create an array automaticly if it does not exsits – it seems. php.net/manual/en/… –  mikaelb Mar 9 '12 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.