Table of Contents

2. Arrays & Collections — PHP’s Most Misunderstood Feature

Why this matters

PHP arrays are powerful — and confusing.
We find them in:

But AI often misuses arrays by:

This page gives us the mental model to recognize when arrays are appropriate
— and when they’re a smell of redundancy.


1. Arrays are both lists and maps

$names = ["Alice", "Bob"];        // list
$user  = ["id" => 1, "name" => "Alice"]; // map

This dual nature is convenient but dangerous.
Modern PHP encourages clarity such as:

Why?


2. Arrays have no fixed shape

Unlike objects, arrays:

This flexibility is useful, but it makes arrays fragile.

If AI returns arrays with inconsistent keys, our mental model will flag the code.


3. Prefer objects for domain concepts

If something has meaning — a User, an Order, a Product —
it should be a class, not an array
because arrays are for:

While objects are for:


4. Typed properties make objects safer than arrays

class User {
    public int $id;
    public string $name;
}

Objects enforce shape.
Arrays do not.

This is one of the clearest signs of modern PHP.


5. Collections deserve their own types

Frameworks often define the following:

These gives us:

AI rarely generates these on its own
— but our understanding helps us guide it.


6. Arrays are fine — until they aren’t

Let's use arrays when:

And use objects when:

This distinction is one of the most important skills in modern PHP.