(PHP 8 >= 8.2.0)
此注解用于标记 class,允许动态属性。
注意:
虽然注解本身不会被继承,但
AllowDynamicProperties注解的效果会被继承。使用此注解标记的类,其子类即使没有显式声明该注解, 也同样允许动态属性。
从 PHP 8.2.0 起弃用动态属性,因此在不使用此注解标记类的情况下使用动态属性将发出弃用通知。
示例 #1 AllowDynamicProperties 与不存在的属性
<?php
class DefaultBehaviour { }
#[\AllowDynamicProperties]
class ClassAllowsDynamicProperties { }
$o1 = new DefaultBehaviour();
$o2 = new ClassAllowsDynamicProperties();
$o1->nonExistingProp = true;
$o2->nonExistingProp = true;
?>以上示例在 PHP 8.2 中的输出:
Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 10
示例 #2 继承类中的 AllowDynamicProperties 与不存在的属性
<?php
class DefaultBehaviour { }
#[\AllowDynamicProperties]
class ClassAllowsDynamicProperties { }
class InheritedClassAllowsDynamicProperties extends ClassAllowsDynamicProperties { }
$o1 = new DefaultBehaviour();
$o2 = new InheritedClassAllowsDynamicProperties();
$o1->nonExistingProp = true;
$o2->nonExistingProp = true;
?>以上示例在 PHP 8.2 中的输出:
Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 12