Skip to content

Instantly share code, notes, and snippets.

@mamchenkov
Created September 8, 2016 12:37
Show Gist options
  • Save mamchenkov/4fc1935ae12fe50c0fd91df02a97c4cf to your computer and use it in GitHub Desktop.
Save mamchenkov/4fc1935ae12fe50c0fd91df02a97c4cf to your computer and use it in GitHub Desktop.
<?php
interface LoggerInterface {
public function __construct($prefix = null);
public function log($message);
}
class Log1 implements LoggerInterface {
protected $prefix;
public function __construct($prefix = __CLASS__) {
$this->prefix = $prefix;
}
public function log($message) {
print $this->prefix . ' : ' . $message . "\n";
}
}
class Log2 implements LoggerInterface {
protected $prefix;
public function __construct($prefix = __CLASS__) {
$this->prefix = $prefix;
}
public function writeLog($message) {
print 'not ' . $this->prefix . ' : ' . $message . "\n";
}
public function log($message) {
return $this->writeLog($message);
}
}
class Useful {
protected $logger;
public function __construct(LoggerInterface $logger = null) {
if (empty($logger)) {
$logger = new Log1('useful');
}
$this->logger = $logger;
}
public function make($something) {
$this->logger->log($something);
}
}
/////////
$useful = new Useful();
$useful->make('money');
$logger2 = new Log2('foobar');
$logger2->writeLog('now for something completely different');
$notuseful = new Useful('something');
$notuseful->make('loss');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment