pecl/ecma_intl

pecl/ecma_intl is a PHP implementation of the ECMAScript Internationalization API Specification (ECMA-402).

But, wait!

PHP isn’t JavaScript, silly! 🤪

That’s right!

There are a few differences from ECMA-402, but for the most part, things work as one might expect. This means you can read the Intl documentation on MDN and translate it to PHP with a few small changes!

Here are a few of the key differences:

Namespaces:

This implementation uses PHP namespaces. The top-level namespace is Ecma, and most everything is under the Ecma\Intl namespace. This means that Intl.Locale in JavaScript becomes Ecma\Intl\Locale in PHP.

Accessor syntax:

Where JavaScript uses dot notation (.) for property and method access, PHP uses “arrows” made up of the hyphen and greater-than characters (->); for static methods, PHP uses double colon (::). This means Intl.getCanonicalLocales() in JavaScript becomes Ecma\Intl::getCanonicalLocales() in PHP, and locale.baseName in JavaScript becomes $locale->baseName in PHP.

Object model:

JavaScript is a prototype-based language, while PHP is a class-based language. As a result, objects in this implementation have some nuanced differences that diverge from the ECMA-402 standard.

Additionally, JavaScript has generic objects (i.e., {}) it passes around like hashes. The closest equivalent in PHP is an associative array, but we chose to use stronger typing in this implementation. As a result, many of the object hashes defined in ECMA-402 (e.g., the options argument passed to the Intl.Locale constructor) are objects with typed properties in PHP (e.g., Ecma\Intl\Locale\Options).

Contents

Indices and tables


For pecl/ecma_intl at main. Updated on 2024-04-01.
This documentation is licensed under the Creative Commons Attribution 4.0 International license.