Modern PHP uses objects not just for behavior, but for clarity of data.
In contrast, AI often generates arrays everywhere — associative arrays for input, output, configuration, responses, and even domain data (see DTO example below).
But arrays:
DTOs solve this by giving shape to our data.
Understanding when to use objects — and when a DTO is the right tool — helps us keep our code predictable, explicit, and easy to review.
A traditional object bundles:
Example:
class User { public function __construct( public string $name, public string $email, ) {} public function greet(): string { return "Hello, {$this->name}"; } }
Objects are ideal when:
AI often generates objects with no behavior
— which is a sign that a DTO might be more appropriate.
A Data Transfer Object is a simple, structured container for data — no behavior, no logic, no side effects.
Example:
class UserData { public function __construct( public string $name, public string $email, ) {} }
| DTOs are ideal when we | - are passing data between layers, - want type safety, - want predictable structure, - want to avoid “stringly‑typed” arrays |
DTOs make our code self‑documenting.
Associative arrays are flexible — too flexible.
Arrays:
DTOs:
AI often defaults to arrays because
they’re easy to generate.
Our mental model helps us replace them with DTOs
where structure matters.
PHP 8 introduced constructor property promotion:
class ProductData { public function __construct( public int $id, public string $name, public float $price, ) {} }
This turns DTOs into a first‑class citizen of modern PHP:
This is one of the most important modern PHP idioms.
A DTO:
An entity:
AI often confuses these two
— generating DTOs where entities belong, or vice‑versa.
Our understanding helps us keep the architecture clean.
Use a DTO when we need:
DTOs shine when we want clarity without behavior.
Avoid DTOs when:
In those cases, use a real object
— an entity or value object.
AI often defaults to associative arrays for everything — input, output, configuration, even domain data. This works, but it hides structure and makes mistakes harder to see. Our mental model helps us notice when an array should really be a DTO, especially in places where shape, clarity, or type safety matter.
Modern PHP uses objects for behavior and DTOs for structure.
DTOs give us:
Once we internalize this, we can immediately see when AI‑generated code is relying too heavily on arrays — or using objects where a DTO would be cleaner.