array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'class.allowdynamicproperties.php', 1 => 'AllowDynamicProperties', ), 'up' => array ( 0 => 'reserved.attributes.php', 1 => '预定义注解', ), 'prev' => array ( 0 => 'attribute.construct.php', 1 => 'Attribute::__construct', ), 'next' => array ( 0 => 'allowdynamicproperties.construct.php', 1 => 'AllowDynamicProperties::__construct', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'language/predefined/attributes/allowdynamicproperties.xml', ), 'extra_header_links' => array ( 'rel' => 'alternate', 'href' => '/manual/en/feeds/class.allowdynamicproperties.atom', 'type' => 'application/atom+xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 8 >= 8.2.0)
此注解用于标记 class,允许动态属性。
从 PHP 8.2.0 起弃用动态属性,因此在不使用此注解标记类的情况下使用动态属性将发出弃用通知。
<?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