Table of Contents

6. Guiding AI Toward Better Architecture — Patterns, Prompts, and Mental Models

Shaping AI before it drifts
AI doesn’t design architecture
— it follows it
.

When we give it a clear mental model,
it stays inside the shape of modern PHP.
But when the model is vague,
AI fills the gaps with patterns from older PHP or other languages.

This page shows how to guide AI before it drifts,
using patterns, prompts, and architectural cues that keep the collaboration predictable.


1. Begin With Architectural Intent

AI writes better code when
it knows the purpose behind the structure.

Instead of:

"Create a service that sends emails."

Try:

"Create a small, focused service.
One responsibility.
No global state.
No static methods.
Use a constructor for dependencies."

We're not giving instructions
— we're giving architecture
.

AI follows the shape we describe.


2. Define the Data Shape Explicitly

AI responds strongly to shape.

So tell it:

Example:

"This function returns a readonly DTO with three fields: 
id, name, email.
No arrays. No optional fields."

AI will stay inside the boundaries we set.


3. Use Constraints as Architectural Guardrails

Constraints prevent drift.

Examples:

"No static methods."
"No magic methods."
"No associative arrays for data transfer."
"Use enums only when they express meaning."
"Avoid unnecessary abstractions."

These constraints aren’t rules
— they’re signals.
They tell AI what kind of PHP we expect.


4. Give AI the Mental Model You Want It to Use

AI doesn’t know our architectural philosophy unless we tell it.

Examples of mental‑model cues:

"Favor clarity over cleverness."
"Prefer objects with stable shape."
"Use modern PHP 8+ idioms only."
"Avoid mixing eras of PHP."
"Use features only when they express intent."

These cues shape the entire output.


5. Ask for the Structure Before the Code

AI writes better code when it drafts the architecture first.

Ask:

"Outline the structure before writing the implementation."

AI will produce:

Once the structure is right,
the code follows naturally
.


6. Guide AI With Patterns, Not Instructions

AI understands patterns better than step‑by‑step commands.

Examples:

"Use a small, focused service object."
"Use a readonly DTO for input."
"Use a clear boundary between parsing and validation."
"Use composition, not inheritance."

Patterns give AI a direction,
not a script
.


7. Use Iteration to Refine Architecture

AI rarely gets the architecture right on the first try.
But it improves quickly
with small, intentional adjustments.

Examples:

"Simplify this. Remove unnecessary abstractions."
"Rewrite this using PHP 8+ idioms only."
"Replace arrays with a DTO."
"Clarify the boundaries between responsibilities."

Each iteration sharpens the shape.


8. Ask AI to Explain Its Choices

This is one of the most powerful tools we have.

Ask:

"Explain why you structured it this way."

AI will reveal:

Once we see the reasoning,
we can redirect it.


9. Reinforce the Architecture You Want

When AI gets something right, reinforce it:

"Good — this DTO has a clear shape."
"Good — this service has one responsibility."
"Good — this avoids unnecessary abstractions."

Positive reinforcement helps AI internalize our architectural style.


Summary

Guiding AI toward better architecture means:

AI drafts quickly.
We provide the architectural compass
.

Together, we produce code that is clear, modern, and intentional.


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.