In object-oriented design, the chain-of-command pattern is a design pattern consisting of a source of command objects and a series of processing objects.
<?php
interface ChainInterface
{
public function on($command);
}
class ChainCommand
{
private $commands = array();
public function add($command)
{
$this->commands[] = $command;
}
public function run($command)
{
foreach ($this->commands as $data)
{
if ($data->on($command))
return;
}
}
}
class User implements ChainInterface
{
public function on($command)
{
if ($command == 'user')
{
echo 'User Command detected!' . PHP_EOL;
return true;
}
}
}
class Email implements ChainInterface
{
public function on($command)
{
if ($command == 'email')
{
echo 'Email Command detected!' . PHP_EOL;
return true;
}
}
}
$chain = new ChainCommand();
$chain->add(new User());
$chain->add(new Email());
$chain->run('user');
$chain->run('email');
?>