Chapter

SOLID

The five SOLID principles - single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion - explained with small PHP examples.

Design Patterns & Principles 7 Lessons from courses

About this chapter

SOLID is five principles that together push object-oriented code toward being easy to change. In this chapter you'll take them one at a time, each with a small "bad then good" PHP example, and finish by refactoring one messy class using several of them at once.

Lessons from courses

  1. 1 What is SOLID? SOLID is five object-oriented design principles - SRP, OCP, LSP, ISP and DIP. Learn what each stands for and why they make code easier to change.
  2. 2 Single Responsibility Principle (SRP) The Single Responsibility Principle says a class should have one reason to change. Learn SRP with a clear PHP before-and-after refactoring example.
  3. 3 Open/Closed Principle (OCP) The Open/Closed Principle says code should be open to extension but closed to modification. Learn OCP in PHP by replacing a growing switch with new types.
  4. 4 Liskov Substitution Principle (LSP) The Liskov Substitution Principle says a subtype must work anywhere its base type works. Learn LSP with the classic Rectangle/Square PHP example.
  5. 5 Interface Segregation Principle (ISP) The Interface Segregation Principle says prefer many small interfaces over one fat one. Learn ISP in PHP so classes never implement methods they don't use.
  6. 6 Dependency Inversion Principle (DIP) The Dependency Inversion Principle says depend on abstractions, not concretions. Learn DIP in PHP by injecting an interface instead of a concrete class.
  7. 7 SOLID in practice Apply SOLID principles together in one worked PHP refactoring - split responsibilities, invert dependencies and open the code to extension step by step.
Design Patterns & Principles Go to course