Neuron-PHP

ICommand

Command pattern interface for encapsulating executable operations.

The ICommand interface defines the contract for command objects in the command pattern implementation. Commands encapsulate a request as an object, allowing you to parameterize clients with different requests, queue or log requests, and support undoable operations.

Key benefits of the Command pattern:

  • Decouples the invoker from the receiver of the request
  • Supports parameterization of objects with operations
  • Allows queuing, logging, and undo operations
  • Enables macro command composition
  • Supports transactional behavior

Command implementations should:

  • Encapsulate all information needed to perform an action
  • Be stateless or maintain minimal state
  • Handle parameter validation within the execute method
  • Return appropriate results or throw exceptions on failure
Tags
example
// Example command implementation
class EmailCommand implements ICommand
{
    private $mailer;

    public function __construct(IMailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function execute(?array $params = null): mixed
    {
        $to = $params['to'] ?? throw new InvalidArgumentException('Missing recipient');
        $subject = $params['subject'] ?? 'No Subject';
        $body = $params['body'] ?? '';

        return $this->mailer->send($to, $subject, $body);
    }
}

// Usage with command invoker
$command = new EmailCommand($mailer);
$invoker = new Invoker();
$invoker->setCommand($command);
$result = $invoker->execute(['to' => '[email protected]', 'subject' => 'Hello']);

Table of Contents

Methods

execute()  : mixed

Methods

execute()

public execute([array<string|int, mixed>|null $params = null ]) : mixed
Parameters
$params : array<string|int, mixed>|null = null

        
On this page

Search results