/*
* WEB开发笔记 www.chhua.com 每日练习 PHP面向对象编程——单一职责原则(SRP)
前面我写了关于OCP的文字,这里再写一下SRP,其实SRP的主要目的还是提高代码的灵活性,下面通过一个简单的模板引擎来说明一下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
85
86
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> Template<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$vars</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$file</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$file</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">file</span><span style="color: #339933">=</span><span style="color: #000088">$file</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> set<span style="color: #009900">(</span><span style="color: #000088">$vars</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">[</span><span style="color: #000088">$vars</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$value</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> asHtml<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/extract" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">extract</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//把数组拆分成键值变量</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_start" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_start</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//缓存开始</span>
		<span style="color: #b1b100">include</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">file</span><span style="color: #339933">;</span>
		<span style="color: #000088">$string</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_get_contents" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_get_contents</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//得到被抑制的输出</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_end_clean" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_end_clean</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//缓存结束</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$string</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000088">$hello</span><span style="color: #339933">=</span><span style="color: #0000ff">"Hello World!"</span><span style="color: #339933">;</span>
<span style="color: #000088">$tmp</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> Template<span style="color: #009900">(</span><span style="color: #0000ff">"test.php"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//实例化模板</span>
<span style="color: #000088">$tmp</span><span style="color: #339933">-></span><span style="color: #004000">set</span><span style="color: #009900">(</span><span style="color: #0000ff">"hello"</span><span style="color: #339933">,</span><span style="color: #000088">$hello</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//设置变量</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$tmp</span><span style="color: #339933">-></span><span style="color: #004000">asHtml</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出HTML</span>
 
<span style="color: #666666;font-style: italic">/*以下代码是改正后的,单一职责原则之后的模板引擎,分离职责!
 * */</span>
 
<span style="color: #000000;font-weight: bold">class</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/file" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">file</span></a><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//提取单独的模板文件</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$name</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$name</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">name</span><span style="color: #339933">=</span><span style="color: #000088">$name</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> getContents<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//返回文件内容</span>
		<span style="color: #b1b100">return</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/file_get_contents" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">file_get_contents</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">name</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000000;font-weight: bold">class</span> TemData <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//处理单独的模板变量</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$vars</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> set<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">[</span><span style="color: #000088">$var</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$value</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> getData<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span> 
<span style="color: #009900">}</span>
 
<span style="color: #000000;font-weight: bold">class</span> Tpl <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//使用提取的模板类</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$data</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$file</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$file</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">file</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/file" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">file</span></a><span style="color: #009900">(</span><span style="color: #000088">$file</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">data</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> TemData<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> set<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">data</span><span style="color: #339933">-></span><span style="color: #004000">set</span><span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000000;font-weight: bold">function</span> processTemplate<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/extract" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">extract</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">data</span><span style="color: #339933">-></span><span style="color: #004000">getData</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//把数组拆分成键值变量</span>
		<span style="color: #000088">$string</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">file</span><span style="color: #339933">-></span><span style="color: #004000">getContents</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/eval" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">eval</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"?>"</span><span style="color: #339933">.</span><span style="color: #000088">$string</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//把模板文件当PHP代码执行</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> asHtml<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_start" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_start</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//缓存开始</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">processTemplate</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//</span>
		<span style="color: #000088">$string</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_get_contents" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_get_contents</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//得到被抑制的输出</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_end_clean" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_end_clean</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//缓冲结束</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$string</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000088">$hello</span><span style="color: #339933">=</span><span style="color: #0000ff">"Hello World!"</span><span style="color: #339933">;</span>
<span style="color: #000088">$tmp</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> tpl<span style="color: #009900">(</span><span style="color: #0000ff">"test.php"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//实例化模板</span>
<span style="color: #000088">$tmp</span><span style="color: #339933">-></span><span style="color: #004000">set</span><span style="color: #009900">(</span><span style="color: #0000ff">"hello"</span><span style="color: #339933">,</span><span style="color: #000088">$hello</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//设置变量</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$tmp</span><span style="color: #339933">-></span><span style="color: #004000">asHtml</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出HTML</span>
 
<span style="color: #666666;font-style: italic">/*
 *以上的演示,就是PHP面向对象的单一职责原则,还可以进一步的完善和更改,就是和OCP原则相接合,改成$tmp=new tpl(new file,new temData)是不是会更好?呵呵,继续关注我的博客,www.chhua.com
 * */</span>
<span style="color: #000000;font-weight: bold">?></span>