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
mbstring
andiconv
extensions; - the
Normalizer
class and thegrapheme_*
functions; - the
utf8_encode
andutf8_decode
functions from thexml
extension; - the
Collator
,NumberFormatter
,Locale
andIntlDateFormatter
classes; - the
intl_error_name
,intl_get_error_code
,intl_get_error_message
andintl_is_failure
functions; - the
hex2bin
function, theCallbackFilterIterator
,RecursiveCallbackFilterIterator
andSessionHandlerInterface
classes introduced in PHP 5.4; - the
array_column
,boolval
,json_last_error_msg
andhash_pbkdf2
functions introduced in PHP 5.5; - the
password_hash
andpassword_*
related functions introduced in PHP 5.5, provided by theircmaxell/password-compat
package; - the
hash_equals
andldap_escape
functions introduced in PHP 5.6; - the
*Error
classes, theerror_clear_last
,preg_replace_callback_array
andintdiv
functions introduced in PHP 7.0; - the
random_bytes
andrandom_int
functions introduced in PHP 7.0, provided by theparagonie/random_compat
package; - a
Binary
utility class to be used when compatibility withmbstring.func_overload
is required.
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.
To write portable code between PHP5 and PHP7, some care must be taken:
\*Error
exceptions must by 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
.
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.
Polyfills are unit-tested alongside their native implementation so that feature and behavior parity can be proven and enforced in the long run.
This library is released under the MIT license.