Skip to main content

PHP Class Design Guide

About this guide​

A 14-chapter guide for PHP engineers who want to learn class design itself, from first principles to practice. The first half builds up class design principles that do not depend on any framework, and the second half applies those principles to real Laravel code. By the end, the goal is to be able to explain common design breakdowns like the Fat Controller and the Fat Model in your own words, and to decide for yourself how to fix them.

What makes this guide different
  • Two parts: Part 1 (chapters 1-7) covers class design principles in framework-independent PHP; Part 2 (chapters 8-14) applies them to model and service class design in Laravel
  • Focused on judgment: rather than memorizing "the right answer," you practice "why choose this design" through the exercises at the end of each chapter
  • Stops at class design: Part 2 stays at class design in Laravel and does not go into layered implementations from DDD or Clean Architecture (see "What this guide does not cover" below)
  • Modern PHP assumed: targets PHP 8.2 or later (readonly, enum, constructor promotion) and Laravel 11 or later

How this guide is structured​

Chapter outline​

Part 1: Class design principles in PHP (framework-independent)

#ChapterOverview
01IntroductionWhy learn class design. Motivation through real examples of god classes and code that resists change
02PHP class syntax basicsProperties, methods, constructors, visibility modifiers, readonly, constructor promotion, enum
03Encapsulation and information hidingThe danger of public properties, tell-don't-ask, and deciding whether you need getters and setters
04The single responsibility principleKeeping classes small. Signs of a god class and a worked example of splitting responsibilities
05Inheritance and compositionis-a versus has-a, and why composition is preferred over inheritance
06Interfaces and depending on abstractionsDesigning against contracts, as the entry point to dependency inversion
07Cohesion, coupling, and SOLID in reviewWhere Part 1 comes together. Fills in O/L/I and revisits all five SOLID principles through cohesion and coupling

Part 2: Class design in practice with Laravel

#ChapterOverview
08Introducing Part 2What Fat Controller and Fat Model mean, and the scope of this part (class design only)
09Responsibilities of an Eloquent modelHow to decide which responsibilities belong in a model and which do not
10Splitting responsibilities into service classesHow to decide when to extract processing into a Service class as a controller grows
11Dependency injection and the Laravel containerHow to put the interface design from Part 1 into practice in Laravel
12Exception design and validationCustom exception classes, the basics of Form Request, and fail-fast design
13Practice: refactoring class designA before-and-after on a Fat Controller and Fat Model that puts every Part 1 principle to work
14SummaryA checklist for making your own calls at work, and where to go next

Who this is for​

  • PHP engineers who want to learn the basic principles of class design systematically (Part 1 requires no Laravel experience)
  • Laravel engineers who feel the pain of Fat Controllers and Fat Models
  • Anyone who found A Practical Guide to Laravel × DDD × Clean Architecture hard going and wants to shore up the fundamentals first

Prerequisites​

RequirementKnowledgeScope
RequiredBasic PHP syntax (variables, functions, control flow)All 14 chapters
Not requiredExperience with class designAll 14 chapters (this guide starts from the basics)
Part 1: not required / Part 2: requiredLaravel's basic MVC structure, routing, and basic CRUD with EloquentChapter 8 onward (if you are unsure, follow the prerequisite links given at the start of chapter 8)

How to read it​

  • New to class design entirely: read Part 1 in order and check your understanding with the exercises at the end of each chapter
  • Comfortable with the basics, in a hurry to apply them in Laravel: skim Part 1 and start properly at chapter 8
  • Using it as a reference: open the relevant chapter directly from the outline above

What this guide does not cover​

  • DDD tactical patterns (value objects, entities, aggregates, the repository pattern, and so on) — covered by A Practical Guide to Laravel × DDD × Clean Architecture
  • Layered Clean Architecture implementations (the use case layer, the presentation layer, managing transaction boundaries, and so on) — same guide
  • An introduction to the Laravel framework itself (how to write routes, basic Eloquent operations, and so on) — Part 2 assumes you have already used these