|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
[php/value object pattern] problemi
Ciao a tutti, sto leggendo un articolo della rivista "io programmo" che tratta il pattern value object. L'articolo è scritto in ruby ma io lo voglio tradurre in php.
Ho la classe Money che è così: Codice:
<?php
class Money {
public $cents;
function __construct($euros, $cents) {
$this->cents = $euros * 100 + $cents;
}
function add($other_money) {
$this->cents += $other_money->cents;
}
function sub($other_money) {
$this->cents -= $other_money->cents;
}
function __toString() {
return (int)($this->cents / 100) . "," . $this->cents % 100 . "€";
}
}
?>
Codice:
<?php
require_once('money.class.php');
class Account {
public $money;
function __construct($owner, $money) {
$this->owner = $owner;
$this->money = $money;
}
function __toString() {
return $this->owner . " possiede " . $this->money;
}
}
?>
Codice:
<?php
require('account.class.php');
$a = new Account("faggot", new Money(2, 50));
$b = new Account("pirla", new Money(2, 50));
$a->money->add(1, 0);
echo $a;
?>
Ultima modifica di ndakota : 07-06-2008 alle 14:53. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
up please
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
risolto. l'errore era nell'index..
Codice:
<?php
require('account.class.php');
$a = new Account("faggot", new Money(2, 50));
$b = new Account("pirla", new Money(2, 50));
$a->money->add(new Money(1, 0));
echo $a;
?>
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:33.



















