Php 如何更好地扩展这个抽象类

Php 如何更好地扩展这个抽象类,php,Php,我正在尝试为不同的货币(如欧元、美元等)实现一个货币格式化程序类。 我试图创建一个抽象类,并希望从这个类扩展Euro和Doller类 因为我是PHP新手,不知道这是否是实现这种想法的更好方法 abstract class Currency { private $name; private $symbol; private $decimal; private $decimal_point; private $thousand_sep;

我正在尝试为不同的货币(如欧元、美元等)实现一个货币格式化程序类。 我试图创建一个抽象类,并希望从这个类扩展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);
      }
  }

当您有一些子类实现不同的功能时,使用抽象超类是有意义的。例如,您希望以不同的方式显示欧元和美元,然后可以定义display()函数abstract并让子类实现它

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

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

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

我认为您不需要所有这些设置器,比如分隔符、小数点等等,在格式化程序的生命周期中不会改变。如果你想让你的类做的只是格式化货币,我认为你也不需要所有的getter

如果您的类只负责格式化,我认为您不应该将值保留为类字段;也许最好将它作为参数传递给
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);
abstract class Currency {
  abstract private $symbol;
/*...*/
}

class GBP extends Currency {
   private $symbol = "£";
}
然后,您可以这样使用它:

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);
abstract class Currency {
  abstract private $symbol;
/*...*/
}

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

您是否知道PHP5.3与捆绑在一起,从而知道应该能够实现您在这里尝试构建的功能

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

我会使用当前区域设置来显示相关的小数点、千位分隔符等,而只需使用
number\u format()
来显示它

在英国,我们会说12345.67美元<代码>12345.67欧元
12345.67英镑

在法国,他们会说12.345,67美元<代码>12.345,67欧元和
12.345,67英镑

换句话说,格式不取决于货币,而是取决于区域设置

但是,根抽象类Currency是一个好主意-子类可能只需要重写
$symbol
,它们会这样做:

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);
abstract class Currency {
  abstract private $symbol;
/*...*/
}

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

i、 e.不需要getter/setter,因为它不会改变。

我强烈建议使用此解决方案,原因如下:intl扩展依赖于CLDR存储库()中的数据,该存储库()是(几乎)任何国家的官方格式集合。而且:在你的类中包装和维护这些函数要容易得多,而不是为你将来可能要支持的每种货币编写PHP类。只是一个提示:-这是来自开源的intl库,你可以看一下。