點擊這里查看網(wǎng)頁教學(xué)網(wǎng) PHP教程 頻道內(nèi)容上文:《PHP設(shè)計模式介紹》第一章 編程慣用法《PHP設(shè)計模式介紹》第二章 值對象模式在所有的最簡單的程序中,大多數(shù)對象都有一個標(biāo)識,一個重要的商業(yè)應(yīng)用對象,例如一個Customer或者一個SKU,有一個或者更多的屬性---id,name,email地址,這樣可以把它從同一個類的其他實例區(qū)分開來。此外,對象有一個恒定的標(biāo)識:它是貫穿于整個html' target='_blank'>應(yīng)用程序的一個唯一的標(biāo)識,對于程序員來說, customer A 在任何地方就是 customer A ,并且只要你的程序在持續(xù)運行時 customer A 仍然是 customer A 。 但是一個對象不需要有一個標(biāo)識。有些對象僅僅是為了描述其他對象的屬性。例如:通常用一個對象描述一個日期、一個數(shù)字或者貨幣。日期、整數(shù)或美元的類定義是都是便于使用的、快捷、便于封裝的,并且方便進(jìn)行拷貝,相互比較,甚至是創(chuàng)建。從表面上看,這些描述簡單的對象很容易被執(zhí)行:它們的語句非常少,在構(gòu)造類時無論是應(yīng)用于Customer還是SKU都沒有什么不同。這個想法似乎是正確的,但是所謂的 似乎正確 很容易產(chǎn)生一些bug。請看下面的代碼,這是一個關(guān)于以美元給員工發(fā)放工資的對象的定義和執(zhí)行操作。多數(shù)情況下,它的運行是沒有問題的。(這個類被命名為BadDollar,因為它還存在著bug)。考慮一下,看你是否能發(fā)現(xiàn)它的bug。// PHP5 class BadDollar { protected $amount; public function __construct($amount=0) { $this- amount = (float)$amount; } public function getAmount() { return $this- amount; } public function add($dollar) { $this- amount += $dollar- getAmount(); } }class Work { protected $salary;public function __construct() { $this- salary = new BadDollar(200);} public function payDay() { return $this- salary; } } class Person { public $wallet; }function testBadDollarWorking() { $job = new Work; $p1 = new Person; $p2 = new Person; $p1- wallet = $job- payDay(); $this- assertEqual(200, $p1- wallet- getAmount()); $p2- wallet = $job- payDay(); $this- assertEqual(200, $p2- wallet- getAmount()); $p1- wallet- add($job- payDay()); $this- assertEqual(400, $p1- wallet- getAmount()); //this is bad actually 400 $this- assertEqual(200, $p2- wallet- getAmount()); //this is really bad actually 400 $this- assertEqual(200, $job- payDay()- getAmount()); }那么, bug是什么呢?如果不能上面的代碼例子中直觀地發(fā)現(xiàn)問題,這里有個提示:雇員對象$p1和對象$p2使用著同一個BadDollar對象實例。首先,類Work和類Person的實例已經(jīng)創(chuàng)建。那么,假設(shè)每一個雇員最初有一個空的電子錢包,雇員的電子錢包Person:wallet是通過Work::payDay()函數(shù)返回的對象資源變量賦值的,所以被設(shè)定為一個BadDollar類的對象實例。還記得PHP5的對象賦值處理方式嗎?因為PHP5的對象賦值的處理方式,所以$job::salary,、$p1::wallet和$p2::wallet這三個看上去不同的對象實例雖然使用著不同的 標(biāo)識符 ,但是事實上,它們?nèi)慷贾付ǖ酵粋€對象實例。因此,接下來的發(fā)放工資的操作(PayDay表示發(fā)放工資的日子,這里表示發(fā)放工資的動作),使用$job- payDay()本來僅僅是想增加$P1的工資,卻出乎意料地次給$P2也發(fā)放了。并且,這個動作還改變了工作的基本工資的額度。因此,最后兩個值的檢測報錯。Value Object PHP5 Unit Test 1) Equal expectation fails because [Integer: 200] differs from [Float: 400] by 200 in testBadDollarWorking in ValueObjTestCase 2) Equal expectation fails because [Integer: 200] differs from [Float: 400] by 200 in testBadDollarWorking in ValueObjTestCase FAILURES!!!html教程