interface Logger { public function execute($message);}class LogToFile implements Logger { public function execute($message){ //code... }}class LogToDb implements Logger { public function execute($message){ //code... }}class UsersController { protected $logger; // Over interface not implementation public function __construct(Logger $logger){ $this->logger = $logger; } public function show(){ $user = 'Juan'; // The execute method will depend over which concrete class $this->logger->execute($user); }}$controller1 = new UsersController(new LogToFile);$controller1->show();$controller2 = new UsersController(new LogToDb);$controller2->show();
Want to create your own Notes for free with GoConqr? Learn more.