Find file History
Type Name Latest commit message Commit time
Failed to load latest commit information.
Tests Replaced with strict assertions Sep 29, 2018
uml Added Behavioral pattern UMLs Jul 31, 2014
AddMessageDateCommand.php PHP7 Command Sep 22, 2016
CommandInterface.php PHP7 Command Sep 22, 2016
HelloCommand.php PHP7 Command Sep 22, 2016
Invoker.php PHP7 Command Sep 22, 2016
README.rst Improve grammar: these code > this code Apr 4, 2017
Receiver.php PHP7 Command Sep 22, 2016
UndoableCommandInterface.php PHP7 Command Sep 22, 2016




To encapsulate invocation and decoupling.

We have an Invoker and a Receiver. This pattern uses a "Command" to delegate the method call against the Receiver and presents the same method "execute". Therefore, the Invoker just knows to call "execute" to process the Command of the client. The Receiver is decoupled from the Invoker.

The second aspect of this pattern is the undo(), which undoes the method execute(). Command can also be aggregated to combine more complex commands with minimum copy-paste and relying on composition over inheritance.


  • A text editor : all events are Command which can be undone, stacked and saved.
  • Symfony2: SF2 Commands that can be run from the CLI are built with just the Command pattern in mind
  • big CLI tools use subcommands to distribute various tasks and pack them in "modules", each of these can be implemented with the Command pattern (e.g. vagrant)

UML Diagram

Alt Command UML Diagram


You can also find this code on GitHub


.. literalinclude:: CommandInterface.php
   :language: php


.. literalinclude:: HelloCommand.php
   :language: php


.. literalinclude:: Receiver.php
   :language: php


.. literalinclude:: Invoker.php
   :language: php



.. literalinclude:: Tests/CommandTest.php
   :language: php