Cannot use [] for reading
The error Fatal error: Cannot use [] for reading is a classic PHP syntax trap that often catches developers off guard when refactoring legacy code or handling dynamic data structures. It stems from a fundamental distinction in how PHP interprets square brackets depending on the context of the assignment operator.
In your specific case, you are attempting to execute this line:
$data[] = self::get($row['sr_id']);
PHP throws the fatal error because the left-hand side of the assignment ($data[]) is being interpreted as an attempt to read from an index rather than write to one. This happens when PHP cannot determine that you are initializing a new array element or appending to an existing one.
The Root Cause: Initialization vs. Access
The syntax $array[] is valid for writing only if the variable $array has already been declared and exists as an array. If $data is undefined at the point of execution, PHP treats [] not as a shorthand for "push this into the end," but as a request to access a specific (and non-existent) index within a non-array context.
When you try to assign a value to $data[] without ensuring $data is an array first, PHP sees the empty brackets and attempts to read the value at that index. Since no array exists, it triggers the "Cannot use [] for reading" error.
The Solution: Explicit Declaration
To resolve this, you must explicitly initialize the variable as an array before attempting to append or access elements via the empty bracket syntax. There are two primary ways to handle this based on your logic flow.
If you know that $data should be an array from the start, declare it at the top of the scope:
$data = []; // Initialize as empty array
$data[] = self::get($row['sr_id']);
This approach is straightforward and ensures the variable exists as an array before the assignment operator processes the [] syntax.
Alternatively, if you want to avoid declaring the array beforehand, you can use the += operator or simply assign directly without brackets if you are creating a new key:
// Using += to append to an existing array (if $data already exists)
$data += [self::get($row['sr_id'])];
// Or simply re-assigning if overwriting is acceptable
$data = [$result];
However, the most robust pattern for appending dynamic objects returned by self::get remains initializing the empty array and using the push syntax ([]).
Handling Dynamic Returns
Your source material notes that self::get returns either a boolean or an object. This introduces a secondary consideration: data integrity within your collection. If self::get fails (returns false), you might end up storing false values in your $data array alongside objects.
While PHP allows mixed types in arrays, this often leads to logic errors later when iterating over $data. Consider filtering out failed requests before appending:
$result = self::get($row['sr_id']);
if ($result !== false) {
$data[] = $result;
}
This ensures that $data strictly contains the objects loaded from the MySQL database, preventing type-mismatch issues during subsequent processing.
Why This Matters in Large Scripts
In large scripts where memory management and data structure integrity are critical, relying on implicit array initialization can lead to subtle bugs. The error you encountered is a safety mechanism preventing you from treating a non-array variable as an array container. By explicitly declaring the type (via []), you make your intent clear to both the PHP interpreter and future maintainers of the code.
If you are building complex data aggregators in Laravel, consider leveraging collections for cleaner syntax that handles initialization implicitly:
use Illuminate\Support\Collection;
$collection = new Collection();
$collection->push(self::get($row['sr_id']));
However, for raw arrays used in high-performance loops or legacy scripts, the explicit [] declaration remains the most efficient and readable solution.