PHP中的连贯操作看起来的确很酷,也非常的方便代码的阅读,当然了必须是在OOP中用才行,在过程化的程序中,就没有必要用这种方法了。有实现这个方法的有用_CALL来实现的,而我下面写的这个例子,则不是用_call的,大家可以扩展一下吧。

下面写的这个SQL语句组合类,主要是用于学习的,如果有同学想拿去用,请再完善一下。

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
<span style="color: #666666;font-style: italic">/*
 * SQL语句组合实例类,始发文章web开发笔记
 * www.chhua.com
 * 学习用,非专业类
 * */</span>
<span style="color: #000000;font-weight: bold">class</span> sql<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$sql</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: #0000ff">"from"</span><span style="color: #339933">=></span><span style="color: #0000ff">""</span><span style="color: #339933">,</span>
			<span style="color: #0000ff">"where"</span><span style="color: #339933">=></span><span style="color: #0000ff">""</span><span style="color: #339933">,</span>
			<span style="color: #0000ff">"order"</span><span style="color: #339933">=></span><span style="color: #0000ff">""</span><span style="color: #339933">,</span>
			<span style="color: #0000ff">"limit"</span><span style="color: #339933">=></span><span style="color: #0000ff">""</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> from<span style="color: #009900">(</span><span style="color: #000088">$tableName</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">sql</span><span style="color: #009900">[</span><span style="color: #0000ff">"from"</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #0000ff">"FROM "</span><span style="color: #339933">.</span><span style="color: #000088">$tableName</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</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> where<span style="color: #009900">(</span><span style="color: #000088">$_where</span><span style="color: #339933">=</span><span style="color: #0000ff">'1=1'</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">sql</span><span style="color: #009900">[</span><span style="color: #0000ff">"where"</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #0000ff">"WHERE "</span><span style="color: #339933">.</span><span style="color: #000088">$_where</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</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> order<span style="color: #009900">(</span><span style="color: #000088">$_order</span><span style="color: #339933">=</span><span style="color: #0000ff">'id DESC'</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">sql</span><span style="color: #009900">[</span><span style="color: #0000ff">"order"</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #0000ff">"ORDER BY "</span><span style="color: #339933">.</span><span style="color: #000088">$_order</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</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> limit<span style="color: #009900">(</span><span style="color: #000088">$_limit</span><span style="color: #339933">=</span><span style="color: #0000ff">'30'</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">sql</span><span style="color: #009900">[</span><span style="color: #0000ff">"limit"</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #0000ff">"LIMIT 0,"</span><span style="color: #339933">.</span><span style="color: #000088">$_limit</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</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> select<span style="color: #009900">(</span><span style="color: #000088">$_select</span><span style="color: #339933">=</span><span style="color: #0000ff">'*'</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #0000ff">"SELECT "</span><span style="color: #339933">.</span><span style="color: #000088">$_select</span><span style="color: #339933">.</span><span style="color: #0000ff">" "</span><span style="color: #339933">.</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/implode" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">implode</span></a><span style="color: #009900">(</span><span style="color: #0000ff">" "</span><span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">sql</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$sql</span> <span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> sql<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
<span style="color: #b1b100">echo</span> <span style="color: #000088">$sql</span><span style="color: #339933">-></span><span style="color: #004000">from</span><span style="color: #009900">(</span><span style="color: #0000ff">"testTable"</span><span style="color: #009900">)</span><span style="color: #339933">-></span><span style="color: #004000">where</span><span style="color: #009900">(</span><span style="color: #0000ff">"id=1"</span><span style="color: #009900">)</span><span style="color: #339933">-></span><span style="color: #004000">order</span><span style="color: #009900">(</span><span style="color: #0000ff">"id DESC"</span><span style="color: #009900">)</span><span style="color: #339933">-></span><span style="color: #004000">limit</span><span style="color: #009900">(</span><span style="color: #cc66cc">10</span><span style="color: #009900">)</span><span style="color: #339933">-></span><span style="color: #004000">select</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #666666;font-style: italic">//输出 SELECT * FROM testTable WHERE id=1 ORDER BY id DESC LIMIT 0,10</span>