Appearance
How to add property to object in PHP >= 5.3 strict mode without generating
In PHP, objects allow dynamic properties by default. However, starting with PHP 8.2, adding undeclared properties is deprecated. To explicitly allow dynamic properties on a class without generating a deprecation warning, apply the #[AllowDynamicProperties] attribute. If you want to enforce strict property declaration and prevent dynamic properties, simply declare all properties in the class definition and omit the attribute.
For example:
Example of allowing dynamic properties in PHP 8.2+
php
<?php
#[AllowDynamicProperties]
class MyClass
{
public $foo;
}
$obj = new MyClass();
// This will not generate a deprecation warning
$obj->bar = 'baz';
echo $obj->bar; // Outputs: "baz"Keep in mind that relying on dynamic properties can make debugging more difficult. It is generally recommended to declare all properties explicitly in the class definition to ensure type safety and maintainability.