0

如何IntlDateFormatter在 Symfony 4.4 中实现。

参考https://symfony.com/doc/current/components/intl.html我已经安装了symfony/intl

现在当我在我的代码中使用它时:

use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;

    $formatter = new IntlDateFormatter(
        "de-DE",
        IntlDateFormatter::FULL,
        IntlDateFormatter::NONE,
        "Europe/Berlin",
        IntlDateFormatter::GREGORIAN,
        "MMMM YYYY"
    );

我越来越Cannot instantiate abstract class Symfony\Component\Intl\DateFormatter\IntlDateFormatter。我知道抽象类不能被实例化。但我不确定如何实施。

4

1 回答 1

1

文档

Composer 自动在全局命名空间中公开这些类

如果我们检查源代码,这更容易理解。定义的类DateFormatter/IntlDateFormatter.php被标记为抽象的,但是有一个实现Resources/stubs/IntlDateFormatter.php看起来像这样:

use Symfony\Component\Intl\DateFormatter\IntlDateFormatter as BaseIntlDateFormatter;

/**
 * Stub implementation for the IntlDateFormatter class of the intl extension.
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @see BaseIntlDateFormatter
 */
class IntlDateFormatter extends BaseIntlDateFormatter
{
}

简而言之,该类是根命名空间中 builin IntlDateFormatter的替代品:

$fmt = new \IntlDateFormatter(...);
           ^

如果你没有安装扩展,自动加载会自动加载 Symfony 的实现。

于 2020-10-05T17:02:45.673 回答