Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get property from valid stdClass object
    primarykey
    data
    text
    <p>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. </p> <pre><code>$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-&gt;timestamp &gt; $lastUpdate) { array_push($eventArray, $decodedEvent); } } $jsonEvents = json_encode($eventArray); echo $jsonEvents; } else { $fileStream = @fopen($fileName, 'a'); } @fclose($fileStream); </code></pre> <p>This produces the error:</p> <pre><code>Notice:Trying to get property of non-object in C:\****\gameManager.php on line 23 </code></pre> <p>I know that the object is valid in multiple ways. For instance, var_dump is producing this:</p> <pre><code>object(stdClass)#1 (3) { ["name"]=&gt; string(4) "move" ["args"]=&gt; array(3) { [0]=&gt; int(24) [1]=&gt; int(300) [2]=&gt; int(50) } ["timestamp"]=&gt; float(1352223678463) } </code></pre> <p>If I try to access $decodedEvent using <code>$decodedEvent["timestamp"]</code> I get an error telling me that objects can't be accessed as arrays.</p> <p>Also, it does indeed echo proper json, which can only be encoded from a proper object:</p> <pre><code>[{"name":"move","args":[24,300,50],"timestamp":1352223678463}] </code></pre> <p>Am I missing something here, or is PHP misbehaving? Any help is greatly appreciated.</p> <p><strong>EDIT</strong>: Here is the input from the file:</p> <pre><code>{"name":"move","args":[24,300,50],"timestamp":1352223678463} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload