One file, 2,000 lines, and every change breaks something else. A REST route with $wpdb inside, HTML built in the same function, settings read in ten places. This module splits that code into layers you can test, change and explain in a code review.
| Course | PHP for WordPress Developers · Module 8 of 9 · Stage 3 "Production code" |
| Learn | MVC and layers in a plugin, bootstrap and container, repository and service, DTOs |
| How | Short lessons with code from real plugins, IDE practice in the browser, interview questions after every lesson |
The idea in one picture
flowchart TD
H["HTTP layer<br/>REST · admin · hooks"] --> S["Service<br/>business logic"]
S --> RP["Repository<br/>data access"]
RP --> DB[("Database")]
C["Container<br/>bootstrap"] -. builds .-> H
C -. builds .-> S
C -. builds .-> RP
classDef step fill:#f5f3ff,stroke:#777BB4,color:#1e1b4b
classDef key fill:#777BB4,stroke:#4f5b93,color:#ffffff
Each layer talks only to the one below. A second entry point, like WP-CLI, reuses the service without copying code.
Before → after
| Before | After Module 8 |
|---|---|
$wpdb inside a REST callback | A repository behind a service |
| HTML built inside business logic | A view layer |
| Arrays with unknown keys passed around | DTOs and value objects |
| Every class creates its own dependencies | One bootstrap with a container |
You'll be able to answer
- How would you split a 2,000-line plugin file?
- What is the difference between a repository and a service?
- When is a DI container overkill?