PHP: AllowDynamicProperties - Manual
The AllowDynamicProperties attribute
(PHP 8 >= 8.2.0)
Introduction
This attribute is used to mark classes that allow dynamic properties.
Note: Although attributes themselves are not inherited, the effect of the
AllowDynamicPropertiesattribute is inherited. Child classes of a class marked with this attribute will also allow dynamic properties, even if they do not explicitly declare the attribute.
Class synopsis
#[\Attribute]
final
class AllowDynamicProperties
{
}
Examples
Dynamic properties are deprecated as of PHP 8.2.0, thus using them without marking the class with this attribute will emit a deprecation notice.
Example #1 AllowDynamicProperties with non-existing property
<?php
class DefaultBehaviour { }
#[\AllowDynamicProperties]
class ClassAllowsDynamicProperties { }
$o1 = new DefaultBehaviour();
$o2 = new ClassAllowsDynamicProperties();
$o1->nonExistingProp = true;
$o2->nonExistingProp = true;
?>Output of the above example in PHP 8.2:
Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 10
Example #2 AllowDynamicProperties with non-existing property in inherited class
<?php
class DefaultBehaviour { }
#[\AllowDynamicProperties]
class ClassAllowsDynamicProperties { }
class InheritedClassAllowsDynamicProperties extends ClassAllowsDynamicProperties { }
$o1 = new DefaultBehaviour();
$o2 = new InheritedClassAllowsDynamicProperties();
$o1->nonExistingProp = true;
$o2->nonExistingProp = true;
?>Output of the above example in PHP 8.2:
Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 12
Table of Contents
- AllowDynamicProperties::__construct โ Construct a new AllowDynamicProperties attribute instance
Found A Problem?
There are no user contributed notes for this page.