Welcome to the PHP Design Patterns repository, where we delve into the exploration of fundamental design patterns in software development. Design patterns serve as standardized solutions to recurring challenges in software design, offering pre-established blueprints that can be tailored to address common issues in code implementation.
This repository categorizes design patterns into three main groups:
- Creational Patterns: Enhance flexibility and code reuse through object creation mechanisms.
- Structural Patterns: Illustrate how to organize objects and classes into larger, flexible, and efficient structures.
- Behavioral Patterns: Focus on effective communication and the assignment of responsibilities among objects.
All examples in this repository are based on the classic Gang of Four (GoF) design patterns, with PHP implementations inspired by the Refactoring.Guru project.
The primary goal of this project is to demonstrate the practical application of Design Patterns using PHP. However, the concepts explored here are transferable to other programming languages such as Go, Rust, Java, Python, TypeScript, and C#, among others.
The repository has been upgraded to PHP 8.3, and testing is made seamless using Docker. Detailed instructions for executing each pattern via the command line in Docker are provided in the README of each pattern.
Expect periodic updates, with a new pattern added every two or three days. The sequence will begin with Behavioral Patterns, followed by Structural Patterns, and finally, Creational Patterns.
To run the examples, ensure you have Docker installed on your system. Follow the installation instructions for your operating system on the official Docker documentation.
Follow these steps to set up and use Docker:
-
Clone the Repository
git clone https://github.com/eduardoraider/php-design-patterns.git cd php-design-patterns
-
Run Composer Dump
After cloning the repository, it's essential to run Composer to generate the autoload files:
composer dump
-
Build Docker Image
Navigate to the project directory and build a Docker image:
docker build -t php-design-patterns-image .
-
Run Docker Container
Run a Docker container using the image you built:
docker run -it --rm -v $(pwd):/app -w /app --name php-design-patterns-container php-design-patterns-image /bin/bash
-
Access the Pattern Directory
Navigate to the pattern directory (example):
cd src/BehavioralDesignPatterns/ChainOfResponsibility
Launch examples via Docker using the PHP executable:
php index.php
-
Manage Containers
Manage Docker containers using various commands:
-
List all running containers:
docker ps
-
Stop a container:
docker stop container_id
-
Remove a container:
docker rm container_id
-
List all images:
docker images
-
Remove an image:
docker rmi image_id
-
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
Alexander Shvets (@neochief) and Alexey Pyltsyn (@lex111)
🛠 🥋 Software Engineer