An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

Logo - PHP 8.1 Design Patterns

![Licence](https://img.shields.io/badge/licence-MIT-blue)
![Package Stars](https://img.shields.io/badge/stars-%E2%98%85%E2%98%85%E2%98%85%E2%98%85%E2%98%85-yellow)
[![Build Status](https://travis-ci.com/gabrielanhaia/php-circuit-breaker.svg?branch=master)](https://travis-ci.com/gabrielanhaia/php-circuit-breaker)
![Code Coverage](https://img.shields.io/badge/coverage-100%25-green)

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 |
|--------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|
| ![Creational](https://img.shields.io/badge/Creational-light_green) | [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) |
| ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | SOON | [HERE](https://github.com/gabrielanhaia/php-design-patterns/blob/main/docs/diagrams/Structural.md) |
| ![Behavioral](https://img.shields.io/badge/Behavioral-red) | SOON | SOON |

## Patterns implemented + Article

| # | 📖 Pattern | Classification~Type | Code Example / Implementation | ✍️ Article / Post 📚 | Diagram |
|----|-------------------------|--------------------------------------------------------------------|--------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------|
| 1 | Abstract Factory | ![Creational](https://img.shields.io/badge/Creational-light_green) | [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 | ![Creational](https://img.shields.io/badge/Creational-light_green) | [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 | ![Creational](https://img.shields.io/badge/Creational-light_green) | [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 | ![Creational](https://img.shields.io/badge/Creational-light_green) | [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 | ![Creational](https://img.shields.io/badge/Creational-light_green) | [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 | ![Creational](https://img.shields.io/badge/Creational-light_green) | [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 | ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | [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 | ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | [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 | ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | [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 | ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | [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 | ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | [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 | ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | [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 | ![Structural](https://img.shields.io/badge/Structural-8A2BE2) | [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 | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [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 | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [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 | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Iterator) | SOON | SOON |
| 17 | Memento | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Memento) | SOON | SOON |
| 18 | Money | ![X](https://img.shields.io/badge/X-grey) | [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 | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/NullObject) | SOON | SOON |
| 20 | Observer | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [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 | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/State) | SOON | SOON |
| 22 | Command | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Command) | SOON | SOON |
| 23 | Chain of Responsibility | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/ChainOfResponsibility) | SOON | SOON |
| 24 | Interpreter | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Interpreter) | SOON | SOON |
| 25 | Mediator | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [HERE](https://github.com/gabrielanhaia/php-design-patterns/tree/main/src/Mediator) | SOON | SOON |
| 26 | Visitor | ![Behavioral](https://img.shields.io/badge/Behavioral-red) | [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.