1) What have we done?
We just add simple "Logger" object with method "log" like you can see in the following code:
public function log($string)
echo date('Y-m-d H:i - ') . $string . PHP_EOL;
2) How we apply it?
In Client we inject "Logger" for receiver and then we can log particular calling methods.
$receiver = new Receiver();
$logger = new Logger();
* @var Logger
public function doA()
echo 'Receiver doing A' . PHP_EOL;
$this->logger->log('Class Receiver executed method doA');
public function doB()
echo 'Receiver doing B' . PHP_EOL;
$this->logger->log('Class Receiver executed method doB');
* @param \Logger $logger
* @return \Receiver
public function setLogger($logger)
$this->logger = $logger;
3) What are cons?
We just littering "Receiver" with this logging. We definitely want to stay class untouched.
4) How can we do this?
Let's wrap calling of "doA" and "doB into "Command" objects.