Symfony Polyfill
This project backports features found in the latest PHP versions and provides
compatibility layers for some extensions and functions. It is intended to be
used when portability across PHP versions and extensions is desired.
Polyfills are provided for:
- the
apcuextension when the legacyapcextension is installed; - the
ctypeextension when PHP is compiled without ctype; - the
mbstringandiconvextensions; - the
uuidextension; - the
MessageFormatterclass and themsgfmt_format_messagefunctions; - the
Normalizerclass and thegrapheme_*functions; - the
utf8_encodeandutf8_decodefunctions from thexmlextension or PHP-7.2 core; - the
Collator,NumberFormatter,LocaleandIntlDateFormatterclasses; - the
intl_error_name,intl_get_error_code,intl_get_error_messageand
intl_is_failurefunctions; - the
idn_to_asciiandidn_to_utf8functions; - the
hex2binfunction, theCallbackFilterIterator,
RecursiveCallbackFilterIteratorandSessionHandlerInterfaceclasses
introduced in PHP 5.4; - the
array_column,boolval,json_last_error_msgandhash_pbkdf2
functions introduced in PHP 5.5; - the
password_hashandpassword_*related functions introduced in PHP 5.5,
provided by theircmaxell/password-compatpackage; - the
hash_equalsandldap_escapefunctions introduced in PHP 5.6; - the
*Errorclasses, theerror_clear_last,preg_replace_callback_arrayand
intdivfunctions introduced in PHP 7.0; - the
random_bytesandrandom_intfunctions introduced in PHP 7.0,
provided by theparagonie/random_compatpackage; - the
PHP_INT_MINconstant introduced in PHP 7.0, - the
SessionUpdateTimestampHandlerInterfaceinterface introduced in PHP 7.0, - the
is_iterablefunction introduced in PHP 7.1; - a
Binaryutility class to be used when compatibility with
mbstring.func_overloadis required; - the
spl_object_idandstream_isattyfunctions introduced in PHP 7.2; - the
sapi_windows_vt100_supportfunction (Windows only) introduced in PHP 7.2; - the
PHP_OS_FAMILYconstant introduced in PHP 7.2; - the
is_countablefunction introduced in PHP 7.3; - the
array_key_firstandarray_key_lastfunctions introduced in PHP 7.3; - the
hrtimefunction introduced in PHP 7.3; - the
JsonExceptionclass introduced in PHP 7.3; - the
get_mangled_object_vars,mb_str_splitandpassword_algosfunctions
introduced in PHP 7.4; - the
fdivfunction introduced in PHP 8.0; - the
ValueErrorclass introduced in PHP 8.0; - the
FILTER_VALIDATE_BOOLconstant introduced in PHP 8.0;
It is strongly recommended to upgrade your PHP version and/or install the missing
extensions whenever possible. This polyfill should be used only when there is no
better choice or when portability is a requirement.
Compatibility notes
To write portable code between PHP5 and PHP7, some care must be taken:
\*Errorexceptions must be caught before\Exception;- after calling
error_clear_last(), the result of$e = error_get_last()must be
verified usingisset($e['message'][0])instead ofnull !== $e.
Usage
When using Composer to manage your dependencies, you
should not require the symfony/polyfill package, but the standalone ones:
symfony/polyfill-apcufor using theapcu_*functions,symfony/polyfill-ctypefor using the ctype functions,symfony/polyfill-php54for using the PHP 5.4 functions,symfony/polyfill-php55for using the PHP 5.5 functions,symfony/polyfill-php56for using the PHP 5.6 functions,symfony/polyfill-php70for using the PHP 7.0 functions,symfony/polyfill-php71for using the PHP 7.1 functions,symfony/polyfill-php72for using the PHP 7.2 functions,symfony/polyfill-php73for using the PHP 7.3 functions,symfony/polyfill-php74for using the PHP 7.4 functions,symfony/polyfill-php80for using the PHP 8.0 functions,symfony/polyfill-iconvfor using the iconv functions,symfony/polyfill-intl-graphemefor using thegrapheme_*functions,symfony/polyfill-intl-idnfor using theidn_to_asciiandidn_to_utf8functions,symfony/polyfill-intl-icufor using the intl functions and classes,symfony/polyfill-intl-messageformatterfor using the intl messageformatter,symfony/polyfill-intl-normalizerfor using the intl normalizer,symfony/polyfill-mbstringfor using the mbstring functions,symfony/polyfill-utilfor using the polyfill utility helpers.symfony/polyfill-uuidfor using theuuid_*functions,
Requiring symfony/polyfill directly would prevent Composer from sharing
correctly polyfills in dependency graphs. As such, it would likely install
more code than required.
Design
This package is designed for low overhead and high quality polyfilling.
It adds only a few lightweight require statements to the bootstrap process
to support all polyfills. Implementations are then loaded on-demand when
needed during code execution.
If your project requires a minimum PHP version it is advisable to add polyfills
for lower PHP versions to the replace section of your composer.json.
This removes any overhead from these polyfills as they are no longer part of your project.
The same can be done for polyfills for extensions that you require.
If your project requires php 7.0, and needs the mb extension, the replace section would look
something like this:
{
"replace": {
"symfony/polyfill-php54": "1.99",
"symfony/polyfill-php55": "1.99",
"symfony/polyfill-php56": "1.99",
"symfony/polyfill-php70": "1.99",
"symfony/polyfill-mbstring": "1.99"
}
}
Polyfills are unit-tested alongside their native implementation so that
feature and behavior parity can be proven and enforced in the long run.
License
This library is released under the MIT license.