WEB开发笔记 www.chhua.com 每日练习 PHP控制器使用命令组

让处理程序执行多个类中某个类中的一个方法时,就可以使用命令组,很简单,就是通过GET把类名和方法名提交给PHP处理程序,然后再把提交的变量从HTTP中分离出来,根据它生成方法或类名,然后运行,如下:

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
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> Request <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">$request</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</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: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</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">request</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">initFromHttp</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: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000000;font-weight: bold">function</span> initFromHttp<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">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/empty" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">empty</span></a><span style="color: #009900">(</span><span style="color: #000088">$_POST</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #000088">$_POST</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/empty" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">empty</span></a><span style="color: #009900">(</span><span style="color: #000088">$_GET</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #000088">$_GET</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><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> get<span style="color: #009900">(</span><span style="color: #000088">$name</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//返回表单键名的值</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array_key_exists" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_key_exists</span></a><span style="color: #009900">(</span><span style="color: #000088">$name</span><span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #0000ff">''</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//如果不存在的键名返回空值</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #009900">[</span><span style="color: #000088">$name</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">$name</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: #666666;font-style: italic">//设置Request数组键值</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #009900">[</span><span style="color: #000088">$name</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: #666666;font-style: italic">//</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #666666;font-style: italic">//应用</span>
<span style="color: #000000;font-weight: bold">class</span> test <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//实例类</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> output<span style="color: #009900">(</span><span style="color: #000088">$url</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">echo</span> <span style="color: #000088">$url</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> WebHandler<span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//运行类</span>
	static <span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> run<span style="color: #009900">(</span><span style="color: #000088">$className</span><span style="color: #339933">,</span><span style="color: #000088">$funName</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: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$className</span><span style="color: #339933">&&</span><span style="color: #000088">$funName</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">$cmd</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> <span style="color: #000088">$className</span><span style="color: #339933">;</span>
			<span style="color: #000088">$cmd</span><span style="color: #339933">-></span><span style="color: #000088">$funName</span><span style="color: #009900">(</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: #b1b100">else</span> <span style="color: #009900">{</span>
			<span style="color: #b1b100">return</span> <span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
 
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
 
<span style="color: #000088">$reuqest</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> Request<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
WebHandler<span style="color: #339933">::</span><span style="color: #004000">run</span><span style="color: #009900">(</span><span style="color: #000088">$reuqest</span><span style="color: #339933">-></span><span style="color: #004000">get</span><span style="color: #009900">(</span><span style="color: #0000ff">"action"</span><span style="color: #009900">)</span><span style="color: #339933">,</span><span style="color: #000088">$reuqest</span><span style="color: #339933">-></span><span style="color: #004000">get</span><span style="color: #009900">(</span><span style="color: #0000ff">"cmd"</span><span style="color: #009900">)</span><span style="color: #339933">,</span><span style="color: #000088">$reuqest</span><span style="color: #339933">-></span><span style="color: #004000">get</span><span style="color: #009900">(</span><span style="color: #0000ff">"url"</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000000;font-weight: bold">?></span>
 
<span style="color: #339933"><</span>a href<span style="color: #339933">=</span><span style="color: #0000ff">"?action=test&cmd=output&url=www.chhua.com"</span><span style="color: #339933">></span>WEB开发笔记<span style="color: #339933"></</span>a<span style="color: #339933">></span>

以上的代码看上去虽然很笨,在实际的应用中,也不会用到,不过,就是通过这样的代码显明易见的告诉大家PHP控制器中命令组的使用方法。