https://github.com/gabrielanhaia/php-design-patterns
Learn how to implement the most important Design Patterns into your PHP application. This project uses PHP 8+. it has examples for each Pattern and an Article explaining how to use them step by step, their advantages, and disadvantages.
https://github.com/gabrielanhaia/php-design-patterns
design-patterns php php8
Last synced: 10 months ago
JSON representation
Learn how to implement the most important Design Patterns into your PHP application. This project uses PHP 8+. it has examples for each Pattern and an Article explaining how to use them step by step, their advantages, and disadvantages.
- Host: GitHub
- URL: https://github.com/gabrielanhaia/php-design-patterns
- Owner: gabrielanhaia
- License: mit
- Created: 2021-03-31T19:22:18.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2023-08-21T18:18:47.000Z (over 2 years ago)
- Last Synced: 2025-04-02T07:09:36.036Z (10 months ago)
- Topics: design-patterns, php, php8
- Language: PHP
- Homepage:
- Size: 254 KB
- Stars: 299
- Watchers: 25
- Forks: 34
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README



[](https://travis-ci.com/gabrielanhaia/php-circuit-breaker)

Every week a new Pattern + Article on www.medium.com/@anhaia.gabriel and www.medium.com/devwarlocks
# PHP 8.2+ - Design Patterns
This repository was created to show the implementation of various Design Patterns using PHP 8.2. There is no
dependency on a framework, and the examples are the most real as possible based on my own experiences solving real-life
problems. Applying the design pattern and its concept in any PHP project will be effortless once you learn the design
pattern and its concept.
### Classification/Type
| Type | General Article | Diagrams |
|--------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|
|  | [LEARN HERE 📖](https://medium.com/devwarlocks/comparing-creational-design-patterns-making-the-right-choice-428cc351e926) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Creational.md) |
|  | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md) |
|  | SOON | SOON |
## Patterns implemented + Article
| # | 📖 Pattern | Classification~Type | Code Example / Implementation | ✍️ Article / Post 📚 | Diagram |
|----|-------------------------|--------------------------------------------------------------------|--------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------|
| 1 | Abstract Factory |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/AbstractFactory) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Creational.md#abstract-factory) |
| 2 | Builder |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Builder) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Creational.md#builder) |
| 3 | Factory Method |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/FactoryMethod) | [LEARN HERE 📖](https://medium.com/devwarlocks/factory-method-pattern-php-design-patterns-3d1df7707c29) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Creational.md#factory-method) |
| 4 | Prototype |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Prototype) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Creational.md#prototype) |
| 5 | Singleton |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Singleton) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Creational.md#singleton) |
| 6 | Object Pool |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/ObjectPool) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Creational.md#object-pool) |
| 7 | Adapter |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Adapter) | [LEARN HERE 📖 ](https://medium.com/mestredev/adapter-php-8-75e00034ae48) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md#adapter) |
| 8 | Composite |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Composite) | [LEARN HERE 📖](https://medium.com/devwarlocks/composite-php-desing-patterns-dd01b1d010f7) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md#composite) |
| 9 | Bridge |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Bridge) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md#bridge) |
| 10 | Facade |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Facade) | [LEARN HERE 📖 ](https://medium.com/mestredev/facade-php-8-design-patterns-40b1ef8566b5) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md#facade) |
| 11 | Decorator |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Decorator) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md#decorator) |
| 12 | Flyweight |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Flyweight) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md#flyweight) |
| 13 | Proxy |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Proxy) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md#proxy) |
| 14 | Template Method |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/TemplateMethod) | [LEARN HERE 📖 ](https://medium.com/mestredev/template-method-php-8-a357f3665a4b) | SOON |
| 15 | Strategy |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Strategy) | [LEARN HERE 📖 ](https://medium.com/mestredev/strategy-in-php-8-design-patterns-2044e5ef54ed) | SOON |
| 16 | Iterator |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Iterator) | SOON | SOON |
| 17 | Memento |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Memento) | SOON | SOON |
| 18 | Money |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Money) | [LEARN HERE 📖](https://medium.com/devwarlocks/money-pattern-with-symfony-framework-55eff713b3d5) | SOON |
| 19 | Null Object |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/NullObject) | SOON | SOON |
| 20 | Observer |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Observer) | [LEARN HERE 📖](https://medium.com/devwarlocks/observer-php-design-patterns-a4367b137324) | SOON |
| 21 | State |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/State) | SOON | SOON |
| 22 | Command |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Command) | SOON | SOON |
| 23 | Chain of Responsibility |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/ChainOfResponsibility) | SOON | SOON |
| 24 | Interpreter |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Interpreter) | SOON | SOON |
| 25 | Mediator |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Mediator) | SOON | SOON |
| 26 | Visitor |  | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Visitor) | [LEARN HERE 📖](https://medium.com/devwarlocks/visitor-php-design-patterns-289224470ebd) | SOON |
## Dependencies
- [Docker](https://www.docker.com)
## Running the project
I strongly recommend that you follow the articles (links above) and run them with Docker. It will be much easier, and
you
need to install Docker on your computer, independent of the OS you are using. With a few commands, you will run/test all
the Design Patterns implemented throughout the course.
#### 1. Build the container:
```bash
docker build -t design-patterns .
```
or if you prefer
```bash
make build
```
#### 2. Run the following commands to test the design patterns:
```bash
make help
```
and then you will see the list of commands
#### 3. You need to run the command with the pattern you want to test, for example:
```bash
make strategy
```
#### 4. In case you want to see a short description of the pattern, you can run the `make PATTERN_NAME-help`, for example:
```bash
make strategy-help
```
With that, you should see a one-line description of the pattern.
## Additional Information
- If you take a look at the examples in the blog or inside the folders `/src/{$pattern_name}/` you will notice that
almost
All the examples contain a folder called `components`; those folders are the files/classes used to demonstrate how to
use each pattern. Never consider what is in there as part of the pattern! They are not examples to be followed in your
applications.