DEV Community

spO0q 🐒
spO0q 🐒

Posted on

PHP 8.2: deprecated

PHP 8.2 introduces various modifications, as it's a major release.

This new version seems to deprecate common structures used by developers, so you might want to look into the changelog. Here are some notable ones, to me.

Dynamic properties: deprecated

class Product {
}

$product = new Product();
$product->title = 'The Game Changer';
Enter fullscreen mode Exit fullscreen mode

The above shortcut is not uncommon in PHP scripts, even if it has never been the recommended way.

You'll have to declare your properties now. Otherwise, you'll get a warning in PHP 8.2:

Deprecated: Creation of dynamic property[...]

It's worth mentioning dynamic properties are still valid if you use the built-in stdClass class:

$test = new stdClass();
$test->my_property = 'test';
Enter fullscreen mode Exit fullscreen mode

Devs who might want to keep using dynamic properties in other classes (e.g., custom classes) will have to use the new #[\AllowDynamicProperties] attribute to explicitly allow such usage (opt-in).

String interpolation: deprecated

Don't panic, only a specific syntax will deprecated:

$myvar = "ok";
echo "${myvar}";
Enter fullscreen mode Exit fullscreen mode

The invalid syntax is when the $ sign is outside the curly braces.

In constrast, "{$myvar}" is still valid.

utf8_encode/utf8_decode: deprecated

This deprecation might look weird, as utf8_encode and utf8_decode are massively [mis]used, but many devs ignore that it only encodes/decodes from ISO-8859-1 (Latin1).

Wrap up

Deprecated does not mean fatal error, but it's important to fix such warnings when migrating from an older version of PHP.

Top comments (0)