Deprecated Dynamic Properties in PHP 8.2

TheDevick,New in PHP 8.2

The creation of dynamic properties is deprecated to help avoid mistakes and typos

First of All: What is Dynamic Properties in PHP? 🤔

Let's say we have our Client class in our codebase. An Client object must have Name and Email.

class Client
{
  public function __construct(
    public string $name,
    public string $email
  )
  {
  }
}
 
$client = new Client("Erick", "erick@bilhalba.com.br");
$client->age = 15;

You've probably noticed that we're attributing an age property to the object, but the class Client doesn't have an age property. If we use var_dump (opens in a new tab) in the object, we will note that PHP will create an property in the class, called age with 15 as value.

object(Client)#1 (3) {
  ["name"]=>
  string(5) "Erick"
  ["email"]=>
  string(21) "erick@bilhalba.com.br"
  ["age"]=>
  int(15)
}

In my opinion, it's a functionality that create more bugs than features in our code, and in modern code, we rarely use Dynamic Properties intentionally.

So, why is there Dynamic Properties in PHP, after all? 🤔

Well, some built-in classes, such as stdClass depends of Dynamic Properties to work. An stdClass means an Standard Class (opens in a new tab). An Standard Class is a class without any properties. Then, the Dynamic Properties are used to attribute properties into the object. The Standard Class are used also in Json Decode (opens in a new tab) Function and Object Casting (opens in a new tab).

Dynamic Properties being Deprecated 🙅‍♂️

Well, the PHP Developers Team decided that, in PHP 8.2 (opens in a new tab), the Dynamic Properties will be deprecated.

Remember: A Functionality being deprecated doesn't means that it will stop working. Indeed, it will display an warning saying that this functionality is deprecated, then, in PHP 9.x, the Dynamic Properties will be discontinued. It will stop working.


In our example, if we run the code, it will print in terminal:

Deprecated: Creation of dynamic property Client::$age is deprecated in /app/App.php on line 14

Note that the code still works. This is just a warning to the developer to prepare the code for the new version of PHP

And if I still wanna use Dynamic Properties? 🙋‍♂️

Well, if you still want to use Dynamic Properties in a class, you can use the #[AllowDynamicProperties] attribute. The Standard Class, for example, uses this attribute. In our example:

#[AllowDynamicProperties]
class Client
{
  public function __construct(
    public string $name,
    public string $email
  )
  {
  }
}
 
$client = new Client("Erick", "erick@bilhalba.com.br");
$client->age = 15; // This will no longer be discontinued

Sources ✨

© Erick Bilhalba Abella.