OOP for WordPress

Build plugin code on classes, interfaces and dependency injection instead of a Singleton in every file.

class My_Plugin { public static $instance; } is not OOP. A Singleton in every file, hooks added in the constructor, nothing testable. This module takes the tutorial habits apart and replaces them with interfaces, composition and dependency injection without a framework.

CoursePHP for WordPress Developers · Module 4 of 9 · Stage 2 "Architecture"
LearnClasses, interfaces, traits, composition and dependency injection, with hooks registered outside the constructor
HowShort lessons with code from real plugins, IDE practice in the browser, interview questions after every lesson

The idea in one picture

flowchart LR
  P["Plugin bootstrap"] --> S["Service<br/>depends on an interface"]
  I["Interface<br/>Storage"] -. implemented by .-> O["Options_Storage"]
  P -- "injects" --> O
  O --> S
  classDef step fill:#f5f3ff,stroke:#777BB4,color:#1e1b4b
  classDef key fill:#777BB4,stroke:#4f5b93,color:#ffffff

The service asks for an interface, the bootstrap decides which class it gets. That is dependency injection without a container.

Before → after

BeforeAfter Module 4
::get_instance() in every classObjects created once and passed in
add_action() inside __construct()A separate register() method
Deep inheritance chainsComposition over inheritance
Classes you cannot testDependencies behind interfaces

You'll be able to answer

  • Why is Singleton considered an anti-pattern in WordPress plugins?
  • What is dependency injection and how do you do it without a framework?
  • When do you choose an interface, an abstract class or a trait?