PHP - Possible to call member function of object stored in an array?
I'm new to PHP and spent some time searching for a similar question, but none seemed to quite answer it for me.
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?
Any suggestions?开发者_如何学Python
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->getInteractions() . '] ' . $value->getMessage();
}
//This loop doesn't work. Throws the error mentioned above
for ($i = 0; $i < 10; $i++) {
echo '[' . $statObjs[$i]->getInteractions() . '] ' . $statObjs[$i]->getMessage();
}
Sometimes it is worth writing a simple test, saving to a separate file, and then running script through the PHP interpreter from the command-line. When debugging complicated code, it is easy to bark up the wrong tree. If you are feeling ambitious, you can take it one step further and explore writing unit tests using PHPUnit.
<?php
$statuses = array(
'test1',
'test2',
'test3',
'test4',
'test5',
'test6',
'test7',
'test8',
'test9',
'test10',
'test11'
);
class StatusMessage {
private $status;
function __construct($status) {
$this->status = $status;
}
function getInteractions() {
return $this->status . " interactions";
}
function getMessage() {
return $this->status . " messages";
}
}
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);
//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->getInteractions() . '] ' . $value->getMessage() . "\n";
}
//This loop doesn't work. Throws the error mentioned above
for ($i = 0; $i < 10; $i++) {
echo '[' . $statObjs[$i]->getInteractions() . '] ' . $statObjs[$i]->getMessage() . "\n";
}
?>
Is that the actual code? There are a couple of mistakes.
Three of the 'i' variables in that last loop are missing dollar signs.
$i<10
$statObjs[$i]->
$statObjs[$i]->
You are incrementing $j, when you want to increment $i ($i++)
Also, if there aren't 10 status messages, you may want to count $i < min(10, $statObjs.length)
Hope this helps,
Chris
I also recommend just trying, within the loop
$statusObject = $statObjs[$i];
print_r($statusObject);
and then perhaps running a to see if the object's actually right there. Sometimes I feel php doesn't treat object references as first class objects when you reference them directly from the array, unlike the java or other language you're used to ;).
This also makes your experiment a little more scientific, as your referencing a $variable directly without the $variable['ArrayFluff'] at the end of it.
for ($i = 0; $i < 10; $i++) {
$statusObject = $statObjs[$i];
echo '[' . $statusObject->getInteractions() . '] ' . $statusObject->getMessage();
//I'm fairly confident this will work =]
}
The problem you have stated seems to indicate to me that the array is not set all the way up to 10. Are you also getting undefined key notices (error_reporting(E_ALL)
to check).
If this is the problem, you can use array_slice()
to get the sub-array that you need:
foreach (array_slice($statObjs, 0, 10) as $obj) {
echo "[{$obj->getInteractions()}] {$obj->getMessage()}";
}
Even if $statObjs
has less than 10 indeces, array_slice will return it. Otherwise, it returns only the first 10.
Now that you've incorporated in the suggestions I made in my previous post, that code is now working for me. Are you using PHP 5 or later?
精彩评论