PHP - Possible to call member function of object stored in an array?
<p>I'm new to PHP and spent some time searching for a similar question, but none seemed to quite answer it for me.</p>

<p>I have an array of Objects (a class I created called StatusMessage) and I want to access a member function of each object in the array. I am able to do this using a foreach loop, but I only need to access the first 10 objects (they are sorted) so I am trying to use a for loop. When I make the member function calls, I get a fatal error about not being able to call member functions on non-objects. Do the StatusMessage objects in $statObjs need to be cast or something?</p>

<p>Any suggestions?</p>

<pre><code>function collectStatuses($statusesAPI){

    $statusObjects = Array();   
    foreach($statusesAPI as $status)
    {
      //StatusMessage is a class I created
    array_push($statusObjects, new StatusMessage($status));             
    }           
    return $statusObjects;
}

//$statutuses[data] was populated further up
$statObjs = collectStatuses($statuses[data]);

//This loop works, but prints all StatusMessage objects in $statObjs 
foreach ($statObjs as $value) {
  //getInteractions() and getMessages() are both member functions of the StatusMessage class I created.
  echo '[' . $value-&gt;getInteractions() . '] ' .  $value-&gt;getMessage();
}

//This loop doesn't work. Throws the error mentioned above
for ($i = 0; $i &lt; 10; $i++) {

  echo '[' . $statObjs[$i]-&gt;getInteractions() . '] ' .  $statObjs[$i]-&gt;getMessage();
}
</code></pre>
