I'm having a very interesting problem with PHP. The following code grabs a line from a text file, decodes that text as json into an stdClass object, then puts it into an array conditionally on one of its properties.
$fileStream = @fopen($fileName, 'r+');
$lastUpdate = $_POST['lastUpdate'];
if($fileStream) {
$eventArray = array();
while (($buffer = fgets($fileStream, 8192)) !== false) {
$decodedEvent = json_decode($buffer);
echo var_dump($decodedEvent);
if ($decodedEvent->timestamp > $lastUpdate) {
array_push($eventArray, $decodedEvent);
}
}
$jsonEvents = json_encode($eventArray);
echo $jsonEvents;
}
else {
$fileStream = @fopen($fileName, 'a');
}
@fclose($fileStream);
This produces the error:
Notice:Trying to get property of non-object in C:\****\gameManager.php on line 23
I know that the object is valid in multiple ways. For instance, var_dump is producing this:
object(stdClass)#1 (3) {
["name"]=>
string(4) "move"
["args"]=>
array(3) {
[0]=>
int(24)
[1]=>
int(300)
[2]=>
int(50)
}
["timestamp"]=>
float(1352223678463)
}
If I try to access $decodedEvent using $decodedEvent["timestamp"]
I get an error telling me that objects can't be accessed as arrays.
Also, it does indeed echo proper json, which can only be encoded from a proper object:
[{"name":"move","args":[24,300,50],"timestamp":1352223678463}]
Am I missing something here, or is PHP misbehaving? Any help is greatly appreciated.
EDIT: Here is the input from the file:
{"name":"move","args":[24,300,50],"timestamp":1352223678463}