Page 47      All Pages  All Books
The Value Object Pattern 47
class Player { // ... /**
* make a payment
* @param Dollar $amount the amount to pay
@return Dollar the amount payed */
public function pay($amount) {
$this->savings = $this->savings->debit($amount);
return $amount; } }
Player::pay() returns the $amount paid so the statement in Monopoly::payRent() of $to->collect($from->pay($rent)) works properly. This can help in the future if you refine the “business logic” to not allow a payment greater than the player’s balance. (Such a circumstance would then return the players balance and perhaps raise a “BankruptException” to calculate a mod-ified payment instead of the full amount. The $to player would still want to collect as much as pos-sible from player $from.)
Terminology — Business Logic
Mentioning “business logic” in the context of modeling a board game may seem odd. The business here does not refer to companies engaged in the act of commerce, but rather to the concept of application-specific requirements in the domain the application is addressing. Think of the definition of business as “an immediate task or objective,” as in “What is your business here?”,
Of course, given the problem domain for Monopoly, perhaps the connotations of “business logic” apply just the same.
PHP 4 Sample Code
Unlike PHP 5, PHP 4’s copy-by-value object semantics work naturally with the Value Object pattern. However, because PHP 4 does not support property or method visibility, implementing a Value Object in PHP 4 has its nuances as well.
If you recall, the “Object Handles” section of the Preface of this book presented three “rules” to “nearly always” apply when working with objects in PHP 4 to simulate PHP 5’s object handles:
1. Create objects by reference ($obj =& new Class;)
2. Pass objects by reference (function funct(&$obj_param) {})

Page 47      All Pages  All Books