2. Arrays & Collections — PHP’s Most Misunderstood Feature
Why this matters
PHP arrays are powerful — and confusing.
We find them in:
ordered lists
simple sequences of values
associative maps
key–value structures with no enforced shape
heterogeneous containers
mixed data gathered for convenience rather than design
the backbone of many APIs
especially older libraries and JSON‑style responses
But AI often misuses arrays by:
mixing shapes
returning different keys on different branches
assuming order
relying on positions that aren’t guaranteed
returning inconsistent structures
making the code fragile and hard to reason about
using arrays where objects are expected
losing meaning, validation, and invariants
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:
lists for sequences
maps for structured data
Why?
2. Arrays have no fixed shape
Unlike objects, arrays:
have no schema
nothing enforces which keys must exist
can change shape at runtime
keys can appear, disappear, or shift as the code executes
can mix types
integers, strings, objects — all in the same structure
can be partially filled
some expected keys may be missing, and PHP won’t complain
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:
simple lists
collections of values without identity or behavior
lightweight data
quick structures that don’t justify a full type
temporary structures
things created, transformed, and discarded in one flow
glue code
small bridges between APIs, formats, or layers
While objects are for:
domain modeling
representing concepts that matter to the system
validation
ensuring the data is always in a valid state
invariants
protecting rules that must never be broken
behavior
methods that express what the concept does, not just what it holds
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:
UserCollection
OrderCollection
StringCollection
These gives us:
type safety
the collection guarantees what it contains
predictable behavior
shared methods for filtering, mapping, sorting, and iteration
domain meaning
the collection itself becomes part of the vocabulary of the system
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:
the data is simple
small, flat structures that don’t justify a full type
the structure is temporary
something created and discarded within the same flow
the domain is not involved
no business rules, no invariants, no long‑term meaning
And use objects when:
the data has meaning
it represents something in the domain, not just a bundle of values
the structure must be enforced
certain fields must exist, and the relationships between them matter
the code must be maintainable
future readers need clarity, stability, and a place for behavior to live
This distinction is one of the most important skills in modern PHP.