sourceAccount = $sourceAccount->addRole('SourceAccount', $this); $this->destinationAccount = $destinationAccount->addRole('DestinationAccount', $this); $this->amount = $amount; } /** * Transfer the amount from the source account to the destination account */ function transfer() { $this->sourceAccount->transferOut($this->amount); } } } /** * Roles are defined in a sub-namespace of the context as a workaround for the fact that * PHP doesn't support inner classes */ namespace UseCases\TransferMoney\Roles { use DCI\Role; class SourceAccount extends Role { /** * Withdraw the given amount from this account * @param float $amount * * Note that we could alternatively have retrieved the amount using $this->context->amount * rather than having an $amount parameter here. */ function withdraw($amount) { $this->decreaseBalance($amount); //update transaction log... } /** * Transfer the given amount from ("out" of) this account to the destination account * @param float $amount */ function transferOut($amount) { $this->context->destinationAccount->deposit($amount); $this->withdraw($amount); } } class DestinationAccount extends Role { function deposit($amount) { $this->increaseBalance($amount); //update transaction log... } } } namespace DataObjects { class Account extends \DCI\RolePlayer { protected $balance = 0; function __construct($initialBalance) { $this->balance = $initialBalance; } function getBalance() { return $this->balance; } function increaseBalance($amount) { $this->balance += $amount; } function decreaseBalance($amount) { $this->balance -= $amount; } } }