Skip to content

Instantly share code, notes, and snippets.

@CRTX
Created April 28, 2022 18:33
Show Gist options
  • Save CRTX/41637506d42211bf097b94c3c9004ec1 to your computer and use it in GitHub Desktop.
Save CRTX/41637506d42211bf097b94c3c9004ec1 to your computer and use it in GitHub Desktop.
Decorator Logging Example
<?php
interface StartEngineInterface
{
public function start($arg1, $arg2, $arg3);
}
class Car implements StartEngineInterface
{
private $engine;
public function __construct(Engine $engine)
{...}
public function start($arg1, $arg2, $arg3)
{
//We do the "business" logic here that starts the car.
$this->engine->doSomething($arg1, $arg2, $arg3);
}
}
class CarAuditor implements StartEngineInterface
{
private $car;
private $logger;
//We can inject the Car class here together with a logger
public function __construct(StartEngineInterface $car, LoggerInterface $logger)
{
$this->car = $car;
$this->logger = $logger;
}
public function start($arg1, $arg2, $arg3)
{
$this->car->start($arg1, $arg2, $arg3);
$this->logger->log($arg1, $arg2, $arg3, 'Started the car');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment