Table of Contents
2. Common Patterns AI Uses
How AI tends to write PHP
— the habits, defaults, shortcuts, and biases that show up again and again.
AI writes PHP with confidence, but it often relies on familiar patterns rather than the deeper shape of the language. These patterns are not “wrong”, but they reveal how AI thinks
— and where it drifts from modern PHP’s architectural intent.
Understanding these patterns helps us guide AI gently back into clarity.
1. The "Array First" Instinct
AI defaults to arrays for almost everything:
- configuration
- data transfer
- nested structures
- return values
- internal state
Example drift:
return [ 'name' => $name, 'email' => $email, 'roles' => ['admin', 'editor'] ];
Modern PHP prefers shape:
- DTOs
- readonly objects
- enums
- typed properties
Arrays are flexible, but they hide intent.
2. Over‑Using Static Methods
AI loves static methods
because they’re easy to call and easy to reason about:
UserFactory::create($data);
But static methods:
- hide dependencies
- reduce testability
- break inversion of control
- encourage procedural thinking
Modern PHP leans toward → objects with clear lifecycles.
3. Mixing Old and New PHP Styles
AI often blends:
- PHP 5‑style classes
- PHP 7‑style type hints
- PHP 8‑style features
- docblocks that duplicate types
Example:
/** @var string $name */ public string $name;
This is syntactically valid but architecturally incoherent.
4. Inventing Patterns From Other Languages
AI frequently imports ideas from:
- JavaScript (async/await, Promises)
- Python (decorators, dataclasses)
- Java (interfaces with default methods)
- TypeScript (structural typing)
These patterns “look right” to the model
but don’t exist in PHP.
5. Over‑Engineering Simple Problems
AI sometimes produces:
- factories for trivial objects
- service locators
- unnecessary interfaces
- abstract classes with one implementation
- dependency injection containers inside functions
This is noise, not architecture.
Modern PHP favors clarity over ceremony.
6. Using Modern Features Decoratively
AI loves:
- attributes
- readonly
- enums
- union types
…but often uses them without purpose.
Examples:
- readonly on objects that mutate
- attributes that don’t map to any system
- enums used as fancy constants
- union types where a DTO is clearer
Modern features express intent, not decoration.
7. Forgetting About Data Shape
AI often:
- returns inconsistent structures
- mixes associative and numeric arrays
- nests arrays too deeply
- forgets to validate input
- changes shape between calls
Modern PHP is about predictable shape.
8. Missing the Edges of the Language
AI sometimes:
- ignores edge cases
- assumes strict typing where none exists
- forgets about nullability
- mishandles exceptions
- misuses error suppression
- invents nonexistent functions
These slips are subtle but important.
Summary
AI tends to:
- default to arrays
- overuse static methods
- mix eras of PHP
- import patterns from other languages
- over‑engineer simple problems
- misuse modern features
- lose data shape
- miss edge cases
These patterns are not failures
— they are signals.
Once we recognize them, we can guide AI back into the shape of modern PHP with clarity and confidence.
This page describes common behaviors observed in AI models when generating PHP code.
These patterns are not guarantees, but they are consistent enough to be useful when reviewing or guiding AI output.
