New in Symfony Polyfills 1.15
March 30, 2020 • Published by Javier Eguiluz
Symfony Polyfills provide PHP implementations of certain PHP features so you can use them even if you don't have the right PHP version or PHP extension installed. Symfony Polyfill 1.15 was released recently and these are the most important changes.
PHP 8 polyfill improvements
PHP 8.0, the next major PHP version stated for release on December 3, 2020, will add some interesting features. Some of them have already been added to Symfony PHP 8 polyfill, so you can start using them today regardless of your PHP version.
Added Stringable
interface
Contributed by
Nicolas Grekas
in #224.
Stringable
is a new PHP interface that is added automatically to any object
that implements __toString()
.
It can be used to state that you accept both strings and objects that can be
converted into strings. In PHP 8 you can do that for example with the following
union type: string|Stringable
.
Read more about this interface in the Stringable PHP RFC.
Added get_debug_type()
Contributed by
Nicolas Grekas
in #226.
The new get_debug_type()
function returns the given type of a variable.
It's useful to simplify the code that required using get_class()
and
gettype()
to get the type of some variable:
1 2 3 4 5
// Before
throw new TypeError('Expected '.Foo::class.' got '.(is_object($bar) ? get_class($bar) : gettype($bar)));
// After
throw new TypeError('Expected '.Foo::class.' got '.get_debug_type($bar));
Read more about this function in the get_debug_type() PHP RFC.
Added str_contains()
The new str_contains()
function checks if a string is contained in another
string and returns a boolean value (true/false) whether or not the string was found.
This function can replace strpos()
and strstr()
which are often used for
the same purpose but require code which sometimes is not very intuitive.
Read more about this function in the str_contains() PHP RFC.
Added preg_last_error_msg()
Contributed by
Nico Oelgart
in #231.
The new preg_last_error_msg()
function returns a human-friendly string
representation of the last PCRE regular expression execution error.
In previous PHP versions, you had to convert the error code returned by
preg_last_error()
into a meaningful error message.
UUID polyfill improvements
Contributed by
Grégoire Pineau
and Nicolas Grekas
in #236
and #240.
The UUID polyfill, which is used in the new Symfony Uid component, added support for UUID 3 and UUID 5 types.
In addition, we added support for UUID on 32-bit systems to make the polyfill truly portable.
Fixed PHP preloading
Contributed by
Nicolas Grekas
in #239.
PHP preloading is one of the most important new features introduced in PHP 7.4. This feature allows loading some functions/classes in memory to make them available to all requests, improving the application performance.
In Symfony Polyfill 1.15 we fixed some issues to make sure all polyfills are compatible with PHP preloading.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
https://wiki.php.net/rfc/annotations_v2
However, given that this would be a new language construct, we won't be able to add them to a polyfill. You'll need to upgrade your PHP version to use them.