Created
April 28, 2022 18:33
-
-
Save CRTX/41637506d42211bf097b94c3c9004ec1 to your computer and use it in GitHub Desktop.
Decorator Logging Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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