No Dynamic Properties By Default

Properties never required a definition before usage, just like variables. They could be added at any moment in any object.

In PHP 8.2, this is now a deprecated behavior. The property must be declared before usage. Visibility, type and default value are optional as before, so the requirement is to add the property in the class.

It is also possible to skip that warning by extending explicitly the stdClass; by adding the #[AllowDynamicProperties] attribute or by creating the magic property method __get or __set, depending on the usage.

PHP code

<?php

class x {}

$x = new x;
$x->property = 1;
echo $x->property;

?>

Before

1

After

PHP Deprecated:  Creation of dynamic property x::$p is deprecated

Deprecated: Creation of dynamic property x::$p is deprecated
1

PHP version change

This behavior was deprecated in 8.2

This behavior changed in 9.0

See Also

Error Messages

Analyzer