-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdependInject.php
77 lines (63 loc) · 1.5 KB
/
dependInject.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
69
70
71
72
73
74
75
76
77
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/8/2
* Time: 23:59
* 依赖注入模式
* 实现调用者与被调用者的解耦
*/
abstract class config {
protected $storage;
public function __construct($storage) {
$this->storage = $storage;
}
}
interface params {
public function get($key);
public function set($key,$val);
}
class OnlineConfig extends config implements params {
/**
* @param $key
* @param null $default
* @return null
*/
public function get($key, $default = null) {
if (isset($this->storage[$key])) {
return $this->storage[$key];
}
return $default;
}
/**
* @param $key
* @param $value
*/
public function set($key, $value) {
$this->storage[$key] = $value;
}
}
class Connect{
protected $config;
protected $host;
public function __construct(params $config) {
$this->config = $config;
}
public function conn(){
$host = $this->config->get('host');
echo 'connect to host:'.$host.PHP_EOL;
$port = $this->config->get('port');
echo 'connect to port:'.$port;
//if connn success
$this->host = $host;
}
public function getHost(){
$this->host;
}
}
$configArr = ['host'=>'127.0.0.1'];
$onlineConfig = new OnlineConfig($configArr);
echo $onlineConfig->get('host').PHP_EOL;
$onlineConfig->set('port','3306');
$connObj = new Connect($onlineConfig);
$connObj->conn();