Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nahidulhasan
Last active September 27, 2021 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nahidulhasan/034f5f6187b9886539eddfdf384de5a5 to your computer and use it in GitHub Desktop.
Save nahidulhasan/034f5f6187b9886539eddfdf384de5a5 to your computer and use it in GitHub Desktop.
<?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();
@rubelc04
Copy link

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?

@nahidulhasan
Copy link
Author

nahidulhasan commented Apr 24, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment