Creado por Luis Rojas
hace más de 7 años
|
||
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();
¿Quieres crear tus propios Apuntes gratis con GoConqr? Más información.