sample code for several design patterns in PHP 8



Build Status Documentation Status Donate

Read the Docs of DesignPatternsPHP or Download as PDF/Epub

This is a collection of known design patterns and some sample codes on how to implement them in PHP. Every pattern has a small list of examples.

I think the problem with patterns is that often people do know them but don't know when to apply which.


You should look at and run the tests to see what happens in the example. To do this, you should install dependencies with Composer first:

$ composer install

Read more about how to install and use Composer on your local machine here.

To run the tests use phpunit:

$ ./vendor/bin/phpunit

Using Docker (optional)

You can optionally build and browse the documentation using Docker for Mac, Windows or Linux.

Just run:

$ docker-compose up --build

Go to http://localhost:8080/ to read the generated documentation.

If you want to localize your documentation you can pass the locale as an argument to the docker build:

$ docker-compose build --build-arg language=de
$ docker-compose up


The patterns can be structured in roughly three different categories. Please click on the 📓 for a full explanation of the pattern on Wikipedia.





Localization & Supported Languages

Code Language Documentation
ca Catalan Docs 📓
zh_CN Chinese Docs 📓
nl Dutch Docs 📓
en English Docs 📓
de German Docs 📓
ja Japanese Docs 📓
pl Polish Docs 📓
pt_BR Portuguese-Brazil Docs 📓
ru Russian Docs 📓
es Spanish Docs 📓
es_MX Spanish-Mexican Docs 📓
tr Turkish Docs 📓
bg Bulgarian Docs 📓
fr French Docs 📓
  • Version in other languages?

    Version in other languages?

    Hey! Whats'up?!

    Is there some version of this repo in other language? I've forked this repo yesterday to translate to portuguese.

    To contribute in translation, check Translation Rule/Hint below

    [important] Before your start, re-fork this project or synchronize your repo with upstream!(because i just do some change)

    Translation Rule/Hint

    1. All po files were generated by all README.rst, Only them (explication of patterns) will be translated, Code and comments should be in English and not change. code is always the core of this repo.
    2. Word for Word translation is not needed, you can add your own explication to make translation much easy to understand.
    3. if you didn't found the language you want to translate, take a look at here, and @shangguokan who will add initial files.
    4. You can use po editor or text editor to translate.
    5. It remain some markup in po files, please be careful not to break reST notation.

    example: Russian translation is completed

    #: ../../Creational/AbstractFactory/README.rst:22
    msgid "You can also find these code on `GitHub`_"
    msgstr "在 `GitHub`_ 上查看代码"   (`xxx`_ one underscore here)
    #: ../../Creational/AbstractFactory/README.rst:24
    msgid "AbstractFactory.php"
    msgstr ""                         (do not need translate xxx.php, keep it empty)


    if you finished, make one pull request or by "Russian translation part one two..."(maxi five) i will create project on read the doc and translation will be hosted at{language_slug}/latest/


    | language(click to preview) | translator | | --- | :-: | | ca – Catalan | @torrentalle | | en – English | @domnikl | | es – Spanish | @torrentalle @desarrolla2 | | pt_BR – Brazilian Portuguese | @leleonam @bgsouza @davispeixoto | | ru – Russian | @KIVagant | | zh_CN – Simplified Chinese | @su-xiaolin @yplam |


    It's possible to change english wikipedia link of title to the localized version. three formats are possible:

    //take care of the num of underscore
    #: ../../Behavioral/ChainOfResponsibilities/README.rst:2
    msgid "`Chain Of Responsibilities`__"
    //1. only keep russian title, russian link on russian title
    msgstr "`Цепочка обязанностей <Цепочка_обязанностей>`_" 
    //2. keep two version titles, russian link on russian title, english link on english title
    msgstr "`Цепочка обязанностей <Цепочка_обязанностей>`_ (`Chain Of Responsibilities`__)"
    //3. keep two version titles, russian link on russian title, no english link
    msgstr "`Цепочка обязанностей <Цепочка_обязанностей>`_ (Chain Of Responsibilities)"
    //4. if no russian wikipedia page,so english link on russian title
    msgstr "`Цепочка обязанностей`__" 
    // I think 1 and 3 are best way


    1. How can i add some link or bold on the translation string? you can use reST markup. msgstr "External **hyperlinks**, likePython"
    2. How can i test my translation? first, you don't need test your translations. if you want, the repo use Sphinx + reStructuredText a.install sphinx on your computer b.learn how translation works c.Install sphinx-intl by pip install sphinx-intl or easy_install sphinx-intl. d.execute $ sphinx-intl build e.execute $ make -e SPHINXOPTS="-D language='pt_BR'" html can find html in folder _build
    opened by leonampd 54
  • Restructure the repository

    Restructure the repository

    What are you think to restructure this repository?

    My proposal:

    Create a directory structure with types of Design Patterns, like on my branch restructure:

    |-- Creational
    |   |-- Factory
    |   |   |-- Tests
    |   |-- ...
    |-- Behavioral
    |   |-- ...
    |-- Structural
    |   |-- ...
    opened by tonicospinelli 27
  • Converting to documentation / book

    Converting to documentation / book

    It would be great & very useful if this could be converted to a PDF book & to gh-pages ( something like )

    opened by eddiejaoude 22
  • remove composer.phar

    remove composer.phar

    Standard nowadays

    opened by TomasVotruba 14
  • DesignPatternsPHP available on Read the Docs

    DesignPatternsPHP available on Read the Docs


    | - Q. - | - A. - | | --- | --- | | Tickets | #114 , #115 | | Type | Feature / Documentation | | Techniques | Sphinx + reStructuredText + | | Participants | @domnikl , @eddiejaoude , @aik099 |

    what has changed and will change:

    • [x] use Pandoc to convert all the to README.rst in script
    • [x] use sphinx tag: literalinclude to embed all related php files in the README.rst of every pattern. in script
    • [x] sphinx-quickstart
    • [x] create TOC tree (index of projet)
    • [x] host on Read the docs
    • [x] add Wikipedia link
    • [x] bug fix
    • [x] reorder php embed files (Top-down)
    • [x] revision
    • [x] finish and ready to merge
    Hello everyone, i found the work in #115 is stop, so i try to continue the work of converting this repo to a documentation/book.
    Do you have any suggestions of the version preview?
    opened by shangguokan 13
  • Singleton should be removed

    Singleton should be removed

    As Singleton is considered as an Anti-Pattern, I thing that should be removed. Instead of that, dependency Injection design is best alternative.

    opened by mmoreram 12
  • Vagrant ansible environment setup

    Vagrant ansible environment setup

    No need to install the correct version of PHP etc, this small and simple vagrant & ansible script will build a new VM environment.

    opened by eddiejaoude 11
  • Repository knows about database columns

    Repository knows about database columns

    I have a thought about Repository pattern. It knows too much about the columns name, if I want to create a Storage for a database where created is a keyword, so the name of this column in this DB is created_at. It will break my Repository implementation. Or even so, if in DB 1 my date is in format YYYY-MM-DD and DB 2 my date is as MM-DD-YYYY, my Repository should not know how to transform the returned string from storage to a DateTime, because the String could vary. What you think?

    Update Illustration

    opened by leocavalcante 10
  • Repository pattern

    Repository pattern

    Implemented repository pattern

    opened by andrewnester 10
  • Adding examples

    Adding examples

    Even if most patterns are self-explaining we should provide usage examples. Any thoughts?

    opened by ghost 10
  • Translation error in readthedocs

    Translation error in readthedocs

    It seems like the translations are not working well in the spanish version, ie

    Captura de pantalla 2021-06-14 a la(s) 11 00 49

    opened by ajest 0

League\Pipeline This package provides a pipeline pattern implementation. Pipeline Pattern The pipeline pattern allows you to easily compose sequential

The League of Extraordinary Packages 820 Jun 9, 2021
A simple stateless production rules engine for PHP 5.3+

Ruler Ruler is a simple stateless production rules engine for PHP 5.3+. Ruler has an easy, straightforward DSL ... provided by the RuleBuilder: $rb =

Justin Hileman 961 May 25, 2021
Powerful implementation of the Specification pattern in PHP

RulerZ The central idea of Specification is to separate the statement of how to match a candidate, from the candidate object that it is matched agains

Kévin Gomez 844 Jun 7, 2021
A simple Monad library for PHP

MonadPHP This is a basic Monad library for PHP. Usage Values are "wrapped" in the monad via either the constructor: new MonadPHP\Identity($value) or t

Anthony Ferrara 268 May 10, 2021
Option Type for PHP

PHP Option Type This package implements the Option type for PHP! Motivation The Option type is intended for cases where you sometimes might return a v

Johannes 2.2k Jun 9, 2021
Primitives for functional programming in PHP

Functional PHP: Functional primitives for PHP NOTE: functional-php used to come with a C extension that implemented most of the functions natively. As

Lars Strojny 1.8k Jun 13, 2021
Function composition.

igorw/compose Function composition. Allows you to stitch functions together to form a pipeline. This can be useful if you have to transform data in ma

Igor 81 May 29, 2021
Iteration primitives using generators

Iteration primitives using generators This library implements iteration primitives like map() and filter() using generators. To a large part this serv

Nikita Popov 994 Jun 9, 2021
A Simple PHP Finite State Machine

Finite, A Simple PHP Finite State Machine Finite is a Simple State Machine, written in PHP. It can manage any Stateful object by defining states and t

Yohan Giarelli 1.2k Jun 20, 2021