Forked from nahidulhasan/UsersController_with_Interface.php
Created
September 27, 2021 14:32
-
-
Save syofyanzuhad/ce44ff8beb46bd3b1a4fa9b09b8f2e40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Logger | |
{ | |
public function execute($message); | |
} | |
class LogToDatabase implements Logger | |
{ | |
public function execute($message){ | |
var_dump('log the message to a database :'.$message); | |
} | |
} | |
class LogToFile implements Logger | |
{ | |
public function execute($message) | |
{ | |
var_dump('log the message to a file :'.$message); | |
} | |
} | |
class UsersController | |
{ | |
protected $logger; | |
public function __construct(Logger $logger) | |
{ | |
$this->logger = $logger; | |
} | |
public function show() | |
{ | |
$user = 'nahid'; | |
$this->logger->execute($user); | |
} | |
} | |
$controller = new UsersController(new LogToDatabase); | |
$controller->show(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment