2011-11-02 113 views
3

我正在嘗試爲不同貨幣如Euro,Doller等實現Currency貨幣類。 我試圖創建一個抽象類並希望將Euro和Doller類從這個班。如何以更好的方式擴展這個抽象類

由於我是PHP新手,不知道這是否是更好的實現這種想法的方法。

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); 
     } 
    } 
+0

只是一個提示:http://php.net/manual/en/class.numberformatter.php - 它是從國際圖書館這是開源的,你可以看看。 – hakre

回答

1

我不認爲你需要所有那些setters,因爲分隔符,小數點等在formatter生命期內不會改變。如果你希望你的班級做的是格式化貨幣,我認爲你也不需要所有的獲得者。

如果你的課只負責格式化,我認爲你不應該把價值看作是一個班級領域;作爲一個參數,最好把它傳遞給display()

怎麼是這樣的:

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 = ","; 

    } 
} 

然後,您可以使用它像這樣:

$formattedAmount = new EuroFormatter()->format(123); 
1

使用抽象超類是有意義的,當你有一些功能,將由子類實現不同。例如,您希望以不同的方式顯示歐元和美元,則可以定義display()函數摘要並使子類實現它。

abstract class Currency { 
.. 
.. 
abstract function display(); 
} 

class Dollar extends Currency { 
    function display() { 
     //display dollar 
    } 
} 

class Euro extends Currency { 
    function display() { 
     //display euro 
    } 
} 
2

你知道PHP5.3自帶intl PECL捆綁在一起,從而知道哪些NumberFormatter應該能夠做你想在這裏建立的東西?

<?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"; 
+1

由於以下原因,我強烈建議使用此解決方案:intl擴展依賴於來自CLDR存儲庫(http://cldr.unicode.org/translation/currency-names)的數據,該數據庫是官方格式的集合(幾乎)任何國家。而且:在您的類中包裝和維護這些函數要容易得多,而不是爲將來可能需要支持的每種貨幣編寫PHP類。 – aurora

1

我會使用當前區域顯示相關小數點,千個分隔符等,並只使用number_format()來顯示它。

在英國,我們會說$12,345.67; €12,345.67£12,345.67
在法國,他們會說$12.345,67; €12.345,67£12.345,67
換句話說,格式不依賴於貨幣而是區域設置。

然而根抽象類貨幣是一個好主意 - 子類可能只需要覆蓋$symbol,他們會做這樣這樣的:

abstract class Currency { 
    abstract private $symbol; 
/*...*/ 
} 

class GBP extends Currency { 
    private $symbol = "£"; 
} 

即沒有必要的getter /這是因爲它不會改變。

相關問題