-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfaces.php
68 lines (53 loc) · 1.15 KB
/
interfaces.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
// Interface think of it as a contract.
// Important : Program to an interface not an implementation/conceretion
// Class which impelements an interface can't have a __construct method.
// If there are multiple ways to do the same task, consider using interface.
/**
*
*/
interface Logger
{
public function execute($msg);
}
/**
*
*/
class LogToDB implements Logger
{
public function execute($msg)
{
# code...
echo 'Logged to Db ' . $msg;
}
}
/**
*
*/
class LogToFile implements Logger
{
public function execute($msg)
{
# code...
echo 'Logged to File ' . $msg;
}
}
/**
*
*/
class UserController
{
protected $logger;
function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function show()
{
# code...
$this->logger->execute('John doe');
}
}
$controller = new UserController(new LogToFile);
$controller->show();
?>