I am trying to implement a Currency formater class for different Currencies like Euro, Doller etc.
I have tried to create an abstract class and want to extend Euro and Doller classes from this class.
As I am new to PHP and don’t know if this is a better way to implement the idea like this.
abstract class Currency {
private $name;
private $symbol;
private $decimal;
private $decimal_point;
private $thousand_sep;
function __construct() {
}
function setName($name) {
$this->name = $name;
}
function getName() {
return $this->name;
}
function setSymbol($symbol) {
$this->symbol = $symbol;
}
function getSymbol() {
return $symbol;
}
function setDecimal($decimal) {
$this->decimal = $decimal;
}
function getDecimal() {
return $this->decimal;
}
function setDecimalPoint($decimal_point) {
$this->decimal_point = $decimal_point;
}
function getDecimalPoint() {
$this->decimal_point;
}
function setThousandSeprator($thousand_sep) {
$this->thousand_sep = $thousand_sep;
}
function getThousandSeprator() {
return $this->thousand_sep;
}
function display() {
return $this->symbol . number_format($this->amount, $this->decimal, $this->decimal_point, $this->thousand_sep);
}
}
I don’t think you need all those setters, as separators, decimal points and such won’t change during formatter lifetime. If all you want your class to do is to format currency, I don’t think you need all the getters either.
If your class is responsible only for formatting, I don’t think you should keep value as a class field; maybe better just pass it to
display()as an argument.How about something like this:
Then, you can use it like this: