Design patterns
Typical solutions to common design problems, with PHP code. Each pattern page follows the same path: intent, problem, solution, analogy, structure, code, and when to use it.
Creational
5 patternsDeal with the creation of objects: explicit (constructor) vs implicit (DI, reflection), wholesale vs piecewise.
Factory Methoddemo
Defines an interface for creating an object but lets subclasses decide which class to instantiate.
Abstract Factory
Produces families of related objects without specifying their concrete classes.
Builder
Constructs complex objects step by step, letting the same process produce different representations.
Prototype
Copies existing objects without making code depend on their classes.
Singletondemo
Ensures a class has only one instance and provides a global access point to it.
Structural
7 patternsConcerned with structure, such as class members. Many are wrappers that mimic the underlying interface, which stresses good API design.
Adapter
Lets objects with incompatible interfaces collaborate.
Bridge
Splits a large class or closely related classes into two hierarchies, abstraction and implementation, that vary independently.
Composite
Composes objects into tree structures and lets clients treat individual objects and compositions uniformly.
Decoratordemo
Attaches new behaviours to objects by placing them inside wrapper objects that share the same interface.
Facade
Provides a simplified interface to a library, framework or other complex set of classes.
Flyweight
Fits more objects into RAM by sharing common state between many objects.
Proxy
Provides a substitute that controls access to another object.
Behavioral
11 patternsAll different, with no central theme: they describe how objects communicate and share responsibility.
Chain of Responsibility
Passes a request along a chain of handlers; each decides to process it or pass it on.
Command
Turns a request into a stand-alone object containing all information about the request.
Interpreter
Defines a grammar for a simple language and an interpreter that evaluates sentences in it.
Iterator
Traverses elements of a collection without exposing its underlying representation.
Mediator
Reduces chaotic dependencies between objects by making them communicate through a mediator object.
Memento
Saves and restores an object's previous state without revealing its implementation.
Observerdemo
Defines a subscription mechanism that notifies multiple objects about events in the object they observe.
State
Lets an object alter its behaviour when its internal state changes, as if it changed its class.
Strategydemo
Defines a family of algorithms, puts each in its own class, and makes them interchangeable.
Template Method
Defines the skeleton of an algorithm in a base class and lets subclasses override specific steps.
Visitor
Separates algorithms from the objects on which they operate.