views:

86

answers:

2

In one of my scripts, I try to do the following

$data[] = self::get($row['sr_id']); // <-- line 55

However, PHP does not allow me to do this, giving me this error

Fatal error: Cannot use [] for reading in /file.php on line 55

The self::get function return either a bool, or an object.

Edit: The get function creates a new object which again loads data from a mysql database.

A: 

try:

$data = Array();
$data[] = self::get($row['sr_id']); // <-- line 55

sathia
Not initializing arrays will not cause a Fatal Error.
Russell Dias
it's alright, but if $data was previously an object or a var it may
sathia
This does not change the error
eriktm
then you know it's something on the other side: self::get($row['sr_id']); what does var_dump(self::get($row['sr_id'])); says?
sathia
+3  A: 

Old PHP versions accepted $var[] in expressions, allowed reading out the $var content regardless of syntax. PHP5.1 made that illegal. But sometimes the error is triggered outside of the intented context.
So my guess (again: show more code) is that the preceeding line contains an unfinished expression, to which the $data[] joins.

In case of object attribute you can wrap your $data var into { }, but that doesn't seem to be the problem in your case. (Else there is something in line 54, that you didn't show.) The right hand side can't reasonably trigger the error. Even array accessing [] an integer or object wouldn't trigger that fatal error.

So if nothing helps, just use array_push(). Work around PHP.

mario
Thanks, the `array_push()` function did the trick, now it works. :)
eriktm