Reputation: 315
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);
}
}
Upvotes: 3
Views: 136
Reputation: 7946
I would use the current locale to display the relevant decimal point, thousand separator, etc and just use number_format()
to display it.
In UK we'd say $12,345.67
; €12,345.67
and £12,345.67
.
In France they'd say $12.345,67
; €12.345,67
and £12.345,67
.
In other words the formatting is not dependant on the currency but the locale.
However a root abstract class Currency is a good idea - the subclasses will probably only need to override the $symbol
, and they'd do so like this:
abstract class Currency {
abstract private $symbol;
/*...*/
}
class GBP extends Currency {
private $symbol = "£";
}
i.e. there's no need for a getter/setter for this since it will not change.
Upvotes: 1
Reputation: 13557
Did you know PHP5.3 comes bundled with the intl PECL and thus knows the NumberFormatter which should be able to do what you're trying to build here?
<?php
$value = 9988776.65;
$nf = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
echo $nf->formatCurrency($value, "EUR") . "\n";
echo $nf->formatCurrency($value, "USD") . "\n";
$nf = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $nf->formatCurrency($value, "EUR") . "\n";
echo $nf->formatCurrency($value, "USD") . "\n";
Upvotes: 2
Reputation: 10239
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:
abstract class CurrencyFormatter {
protected $name;
protected $symbol;
protected $decimal;
protected $decimal_point;
protected $thousand_sep;
function format($amount) {
return $this->symbol . number_format($amount, $this->decimal, $this->decimal_point, $this->thousand_sep);
}
}
class EuroFormatter extends CurrencyFormatter {
public function __construct() {
$this->name = "Euro";
$this->symbol = "E";
$this->decimal = 2;
$this->decimal_point = ".";
$this->thousand_sep = ",";
}
}
Then, you can use it like this:
$formattedAmount = new EuroFormatter()->format(123);
Upvotes: 1
Reputation: 3363
Using an abstract superclass makes sense when you have some functionality which would be implemented differently by the subclasses. For example you want to display Euro and Dollar differently, then you can define display() function abstract and have subclasses implement it.
abstract class Currency {
..
..
abstract function display();
}
class Dollar extends Currency {
function display() {
//display dollar
}
}
class Euro extends Currency {
function display() {
//display euro
}
}
Upvotes: 1