How to use `IntlDateFormatter` in Symfony?
Asked Answered
H

1

1

How can I implement IntlDateFormatter in Symfony 4.4.

With reference to https://symfony.com/doc/current/components/intl.html I have installed symfony/intl

Now when I use it in my code:

use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;

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

I am getting Cannot instantiate abstract class Symfony\Component\Intl\DateFormatter\IntlDateFormatter. I know abstract class can not be instantiate. But I am not sure how to implement.

Hipped answered 5/10, 2020 at 13:48 Comment(0)
C
3

From documentation:

Composer automatically exposes these classes in the global namespace

This is easier to understand if we check the source code. The class defined at DateFormatter/IntlDateFormatter.php is marked as abstract, but there's one implementation at Resources/stubs/IntlDateFormatter.php that looks like this:

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

/**
 * Stub implementation for the IntlDateFormatter class of the intl extension.
 *
 * @author Bernhard Schussek <[email protected]>
 *
 * @see BaseIntlDateFormatter
 */
class IntlDateFormatter extends BaseIntlDateFormatter
{
}

In short, the class is a drop-in replacement for builin IntlDateFormatter in the root namespace:

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

If you don't have the extension installed, auto-loading will load Symfony's implementation automatically.

Cytolysin answered 5/10, 2020 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.