WEB开发笔记 www.chhua.com 每日练习 PHP设计模式——值对象模式(PHP中值对象的使用)
说到值对象,不得不说一下对象的引用,对象的引用是将一个对象赋值给一个变量,有很多人认为这是在复制对象,但实际上,只是在复制对象的引用而已,而在更改一个对象的引用时,另一个引用也会跟着更改,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> website <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000088">$url</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: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">url</span><span style="color: #339933">=</span><span style="color: #0000ff">"http://www.chhua.com"</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> addName<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">$name</span><span style="color: #339933">=</span><span style="color: #000088">$name</span><span style="color: #339933">.</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">url</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">url</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> printName<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	    <span style="color: #b1b100">echo</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">url</span><span style="color: #339933">;</span>	
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$web</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> website<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$webTest</span><span style="color: #339933">=</span><span style="color: #000088">$web</span><span style="color: #339933">;</span>
<span style="color: #000088">$webTest</span><span style="color: #339933">-></span><span style="color: #004000">addName</span><span style="color: #009900">(</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$web</span><span style="color: #339933">-></span><span style="color: #004000">printName</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//引用改变之后,被引用的对象也会随着变化.输出的将不会是"http://www.chhua.com",而是“WEB开发笔记:http://www.chhua.com"”。</span>
<span style="color: #000000;font-weight: bold">?></span>

引用改变之后,被引用的对象也会随着变化.输出的将不会是”http://www.chhua.com”,而是“WEB开发笔记”。而我们是希望webTest对象改变,而web对象不改变,因此就用到了值对象模式。看下面的演示:

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
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> website <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$url</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">url</span><span style="color: #339933">=</span><span style="color: #0000ff">"http://www.chhua.com"</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> addName<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">$newWeb</span><span style="color: #339933">=</span>clone <span style="color: #000088">$this</span><span style="color: #339933">;</span>
		<span style="color: #000088">$name</span><span style="color: #339933">=</span><span style="color: #000088">$name</span><span style="color: #339933">.</span><span style="color: #0000ff">":"</span><span style="color: #339933">.</span><span style="color: #000088">$newWeb</span><span style="color: #339933">-></span><span style="color: #004000">url</span><span style="color: #339933">;</span>
		<span style="color: #000088">$newWeb</span><span style="color: #339933">-></span><span style="color: #004000">url</span><span style="color: #339933">=</span><span style="color: #000088">$name</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$newWeb</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> printName<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	    <span style="color: #b1b100">echo</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">url</span><span style="color: #339933">;</span>	
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$web</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> website<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$webTest</span><span style="color: #339933">=</span><span style="color: #000088">$web</span><span style="color: #339933">-></span><span style="color: #004000">addName</span><span style="color: #009900">(</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$web</span><span style="color: #339933">-></span><span style="color: #004000">printName</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//使用值对象之后,输出的将依然是"http://www.chhua.com"</span>
<span style="color: #000088">$webTest</span><span style="color: #339933">-></span><span style="color: #004000">printName</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//这里输出的是“WEB开发笔记:http://www.chhua.com"”。</span>
<span style="color: #000000;font-weight: bold">?></span>

其实很容易理解,值对象就是返回一个带有不可改变的属性的对象而已,呵呵,我的理解是这样的,“有值的对象就是值对象”。
关注面向对象,关注PHP,请记住我的PHP进阶群(63417682)和我的网站www.chhua.com,