-
Notifications
You must be signed in to change notification settings - Fork 1
/
12-空对象模式.php
84 lines (74 loc) · 1.76 KB
/
12-空对象模式.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
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
/*
* This file is modified from `xiaohuangniu/26`.
*
* @see https://github.com/xiaohuangniu/26
*/
header('Content-type: text/html; charset=utf-8');
/**
* 抽象 控制器类.
*/
abstract class Controller
{
public $_name = ''; // 控制器名称
/**
* 注入控制器名.
* @param mixed $name
*/
public function __construct($name)
{
$this->_name = $name;
}
/**
* 输出内容
* $Controller 被访问的控制器对象实例.
* @param mixed $Controller
*/
abstract public function Dump($Controller);
}
/**
* 创建 - 空对象时 返回的对象
*/
class EmptyController extends Controller
{
public function Dump($Controller)
{
echo '访问的控制器不存在,系统即将抛出404错误!';
}
}
/**
* 创建 - 假设这些都是正确的 - 子类控制器.
*/
class YesController extends Controller
{
public function Dump($Controller)
{
echo "‘{$Controller->_name}’ 控制器的,‘{$this->_name}’ 控制器被用户访问了~ ".PHP_EOL;
}
}
/**
* 创建 - 假设这个是父类控制器.
*/
class FuController extends Controller
{
public function Dump($Controller)
{
// 不是对象则 返回空对象
if (!is_object($Controller)) {
$Controller = new EmptyController('');
}
$Controller->Dump($this);
}
}
// 创建父类控制器:Index
$teacher = new FuController('Index');
// 创建子类控制器 1-3
$A = new YesController('子类1');
$B = new YesController('子类2');
$C = new YesController('子类3');
// 开始访问咯
$teacher->Dump($A);
$teacher->Dump($B);
$teacher->Dump($C);
$teacher->Dump('子类4'); // 访问了一个不存在的控制器,抛出404