-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.php
61 lines (56 loc) · 1.33 KB
/
adapter.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
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>适配器模式</title>
</head>
<body>
适配器设计模式只是将某个对象的接口适配为另一个对象所期望的接口
<?php
class errorObject{
private $_error;
public function __construct($error){
$this->_error = $error;
}
public function getError(){
return $this->_error;
}
}
// 所期望对象的类
class logToCSV{
const CSV_LOCATION = 'log.csv';
private $_errorObject;
public function __construct($errorObject){
$this->_errorObject = $errorObject;
}
public function write(){
$line = $this->_errorObject->getErrorNumber();
$line .= ',';
$line .= $this->_errorObject->getErrorText();
$line .= '\n';
file_put_contents(self::CSV_LOCATION,$line,FILE_APPEND);
}
}
// 适配类
class logToCSVAdapter extends errorObject{
private $_errorNumber,$_errorText;
public function __construct($error){
parent::__construct($error);
$parts = explode(":", $this->getError());
$this->_errorNumber = $parts[0];
$this->__errorText = $parts[1];
}
public function getErrorNumber(){
return $this->_errorNumber;
}
public function getErrorText(){
return $this->__errorText;
}
}
// 实例化对象,运行
$error = new logToCSVAdapter("404: Not Found");
$log = new logToCSV($error);
$log->write();
?>
</body>
</html>