Deprecation's in PHP 8.2 and What you have to know

Parvej Ahammad
4 min readJun 12, 2022

--

If you haven’t read my what’s new in php 8.2. You can read here https://medium.com/@parvej.code/feature-that-are-accepted-for-php-8-2-f01722b5a658

The PHP team is bringing new feature and fix within every single release. But it is not always feature and fix. Sometimes the team has to take a hard decision to let go of some features to standardize the language and we call it deprecation. In PHP 8.2 there is also a few deprecation.

Dynamic Properties are deprecated

It is possible in PHP classes to dynamically set and get properties that you don’t have to declare in the class.

class FooBar {
public string $bar;
}

$fooBar = new FooBar();
$fooBar->foo = 'Foo';

You can only prevent this behavior using __set() and __get() magic method.

In PHP 8.2 or later, setting a value to an undeclared class property is deprecated, and emits a deprecation notice the first time the property is set during the lifetime of the application execution.

class FooBar {
public string $bar;
}
$fooBar = new FooBar();
$fooBar->foo = 'Foo';

This will generate a deprecation notice like bellow.

Deprecated: Creation of dynamic property FooBar::$foo is deprecated in ... on line ...

In PHP 9 it will cause fatal-error.

Motivation: Even though dynamic properties in class allows developer to set and retrieve it, it also opens the possibility of potential bug and unexpected behaviors in the application. For example, a typo in a statement that sets a property could go unnoticed because PHP silently allows all dynamic properties.

The problem is a lot of library and framework rely on dynamic properties. Take Laravel Elquent ORM for instance, it’s heavily rely on dynamic properties for it’s properties and relationship. So with this deprecation Laravel framework will face a lot of re-write.

There are three exceptions for this deprecation.

  1. Classes with #[AllowDynamicProperties] attribute.

With this new attribute introduced in PHP 8.2 you can stop PHP from emitting deprecation notice. Even child class of that class will inherit this behavior.

2. stdClass and its sub-classes

stdClass already has #[AllowDynamicProperties] attribute defined so stdClass and any of it sub-class will allow dynamic property

3. Classes with __get and __set magic methods

If a class has __set() magic method defined it will exempt from this deprecation. You can eventually add an __get() method to create a practically useful class.

class FooBar {
public function __set(string $name, mixed $value): void {}
}
$fooBar = new FooBar();
$fooBar->foo = 'Foo';

But setting dynamic properties in __set() still not allowed.

class FooBar {
public function __set(string $name, mixed $value): void {
$this->{$name} = $value;
}
}
$fooBar = new FooBar();
$fooBar->foo = 'foo';

Above is not allowed, cause we are setting dynamic properties in __set() magic method. It will throw same deprecation notice

Deprecated: Creation of dynamic property FooBar::$foo is deprecated in ... on line ...

Partially-supported callable are deprecated

PHP 8.2 deprecates certain patterns of callables that do not work with the $callable() pattern.

Unaffected Callable Patterns

$callable = 'strlen';
$callable = ['MyClass', 'myMethod'];
$callable = 'MyClass::myMethod'];
$callable = Closure::fromCallable();
$callable = [$this, 'myMethod'];
$callable = [new MyClass(), 'myMethod'];
$callable = strlen(...);

These are the callable pattern that will work without any deprecation warning.

Deprecated Callable Patterns

$callable = "self::method";
$callable = "parent::method";
$callable = "static::method";
$callable = ["self", "method"];
$callable = ["parent", "method"];
$callable = ["static", "method"];
$callable = ["MyClass", "MyParentClass::myMethod"];
$callable = [new MyClass(), "MyOtherClass::myMethod"];

So if you try to use callable above way, it will throw a deprecation notice.

Mbstring: Base64, Uuencode, QPrint, and HTML Entity encodings are deprecated

PHP’s Multi-Byte Strings extension (mbstring) adds functionality to manipulate PHP strings that contains multi-byte characters such as characters from Asian scripts, Emojis, and thousands of other characters that cannot be assigned and fit into a single byte.

In PHP 8.2, using Mbstring extension to encode/decode strings to Base64, Quoted-Printable, Uuencode, and HTML Entities is deprecated.

The following labeled encodings are affected. The encoding labels are case-insensitive.

BASE64
UUENCODE
HTML-ENTITIES
html (alias of HTML-ENTITIES)
Quoted-Printable
qprint (alias of Quoted-Printable)

The reason for this is, The PHP core already provide alternative for these function. For example

mb_convert_encoding('test', 'base64'));

You can achieve same thing using base64_encode('test')

Check out this for more

${var} string interpolation deprecated

It is possible in PHP to replace a variable in string literal with double quotes and heredoc syntax.

$name = 'PHP';
echo "Hello $name"; // Hello PHP

For better readability you can also add {} around the variable. But PHP also support dollar sign outside of the curly braces.

echo "Hello ${name}";

But this has been deprecated in php 8.2.

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in ... on line ...

--

--