Practical guide
How to use AI practically when developing PHP applications
Give the model a precise goal, bounded context, and verifiable conditions; treat its output as an untrusted change proposal.
In short
AI speeds up the loop; it does not own the outcome
An LLM is good at finding related code, proposing options, adding test cases, and performing repetitive refactors. It does not automatically know your production data, business context, or which assumption is dangerous.
An AI agent can read files and run tools, so it needs the same least-privilege, isolation, and audit principles as other automation. A human remains responsible for every accepted diff.
Prepare
Create a safe working environment
Before the model gets a task, define what it may read, modify, run, and where it must not send data.
- A repository with a clean worktree or isolated branch and fast tests, linting, and static analysis.
- Project rules: architecture, style, supported PHP, commands, and a definition of done.
- A policy for source code, secrets, personal data, client data, and external model use.
- Restricted tool permissions; production, secrets, and destructive commands require separate approval.
Steps 1 to 3
Work in short, verifiable cycles
One prompt handles one concrete problem and ends with a diff that can be read and checked automatically.
1. Write the task as a technical contract
- State the goal, reason, files or module in scope, constraints, and exact acceptance criteria. Add relevant error output or a minimal reproduction.
- First ask it to find related locations and explain risks briefly. For a large task, split the proposal into independently verifiable steps.
- Provide only necessary context. A long repository dump weakens signal and may accidentally contain secrets or personal data.
- Specify which commands must pass and what must remain unchanged, such as a public API or database schema.
Goal + context + constraints + acceptance criteria + verification NIST AI Risk Management Framework 2. Produce a small diff and verify it
- The model reads the existing implementation and local conventions first. It adds no new abstraction unless the problem needs one.
- After each small diff, it runs syntax checks, relevant PHPUnit tests, PHPStan, and formatting; it analyzes failures instead of hiding them with ignores.
- Review business conditions, error paths, transactions, authorization, types, logging, and backward compatibility. A generated test must genuinely be able to fail.
- Require deeper human review and official documentation for authentication, cryptography, money, migrations, and concurrency.
git diff --check && vendor/bin/phpunit && vendor/bin/phpstan analyse Official PHP security documentation 3. Restrict the agent and finish code review
- Treat issue text, comments, documents, and source code as untrusted data. Instructions hidden in them must not expand the agent’s permissions.
- Start read-only and limit writes to a working branch. Approve network, shell, deletion, production services, and sending messages separately by impact.
- A human performs a full
- glossary-code-reviewcode review
- , verifies sources and licensing of reused code, and decides whether the change meets the original goal.
- Record the task, important decisions, check results, and approver. Do not rely on chat history as a replacement for commits and technical documentation.
read → propose → patch → test → review → merge OWASP GenAI: Prompt Injection Step 4
Measure accepted outcomes, not prompt count
A useful workflow shortens change time without increasing regressions, security exceptions, or review cost.
-
Compare a repeatable task
Track lead time, post-review fixes, escaped defects, and usage; fast generation alone is not an outcome.
-
Insert an invalid assumption
The workflow catches it with a test, type check, or human review before merge.
-
Simulate a malicious issue instruction
The agent treats it as data, gains no secrets, does not widen scope, and requests approval for risky action.
Troubleshooting
Common problems
AI changes too many files
Reduce scope, state forbidden changes, and split the task into one observable outcome with a small diff.
git diff --stat Generated tests always pass
Temporarily violate the rule or use mutation testing. Verify the assertion measures public behavior rather than copying the implementation.
The model invents library APIs
Have it read the locked version and local types, then verify claims in official documentation and run a relevant test.
A secret leaked into a prompt
Revoke and rotate it immediately, inspect logs, and add pre-commit or CI secret scanning and a clearer data policy.
Done
AI is part of a controlled development process.
A precise task, small diff, automated checks, restricted permissions, and human review turn model speed into a safer delivered change.