Here are some alternatives for using the same API .. so this will be the default setting:
interface Printerish // couldn't think of a good ajective { public function print(); } interface CanLogStuff // couldn't think of a good ajective { public function log( $text ); } class Printer implements Printerish { public function print() {
And this will be a loggable printer:
class LoggedPrinter implements Printerish { protected $logger; protected $pritner; public function __construct( Printerish $printer, CanLogStuff $logger ) { $this->logger = $logger; $this->printer = $printer; } protected function print() { $this->logger( 'I can print, I can print !!' ); $this->printer->print(); } }
In this case, the following precedent is used: if in the real world you want to start controlling the use of a real printer (the intern was printing the Internet again). Then you will not make another printer. You would try to add external control.
In programming, this refers to the Open / closed Principle .
Keep in mind that this is just an idea, and you should carefully study this before trying to use it in production code.
tereลกko
source share