AllowDynamicProperties 注解

(PHP 8 >= 8.2.0)

简介

此注解用于标记 class,允许动态属性

注意:

虽然注解本身不会被继承,但 AllowDynamicProperties 注解的效果被继承。使用此注解标记的类,其子类即使没有显式声明该注解, 也同样允许动态属性。

类摘要

#[\Attribute]
final class AllowDynamicProperties {
/* 方法 */
public function __construct()
}

示例

从 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

参见

注解概览

目录

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。