Last active
September 27, 2021 14:32
-
-
Save nahidulhasan/034f5f6187b9886539eddfdf384de5a5 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(); |
Hello,
Here I didn't create the instance of the interface.
Please read about the dependency injection.
I hope your concept will be cleared.
…On Thu, Apr 23, 2020 at 9:33 PM Mobarok Hossain Rubel < ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hello brother, I read your Article in medium.com, Please can you explain
this for me.
**public function __construct(Logger $logger);
$controller = new UsersController(new LogToDatabase);**
Here Logger is Interface, but pass Instance of LogToDatabase class when
creating instance of UserController class. How this is work?
I know that we cannot create instance of an interface. but there passing
Instance of LogToDatabase class, a reciveing it using Logger Interface, How
this is work?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/034f5f6187b9886539eddfdf384de5a5#gistcomment-3266597>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAXESAFIRAAIVYQCEQDRMOLROBNU5ANCNFSM4MPE5SFA>
.
--
Regards,
Nahidul Hasan
Senior Software Engineer
Brain Station 23 Ltd.
Dhaka,Bangladesh.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello brother, I read your Article in medium.com, Please can you explain this for me.
Here Logger is Interface, but pass Instance of LogToDatabase class when creating instance of UserController class. How this is work?
I know that we cannot create instance of an interface. but there passing Instance of LogToDatabase class, a reciveing it using Logger Interface, How this is work?