The AllowDynamicProperties attribute

(PHP 8 >= 8.2.0)

Вступ

This attribute is used to mark classes that allow dynamic properties.

ЗауваТСння: Although attributes themselves are not inherited, the effect of the AllowDynamicProperties attribute 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.

ΠšΠΎΡ€ΠΎΡ‚ΠΊΠΈΠΉ огляд класу

#[\Attribute]
final class AllowDynamicProperties {
/* ΠœΠ΅Ρ‚ΠΎΠ΄ΠΈ */
public function __construct()
}

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ΠΈ

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.

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #1 AllowDynamicProperties with non-existing property

<?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 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;
?>

Π’ PHP 8.2 ΠΏΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡ‰Π΅ ΠΏΡ€ΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:

Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 12

ΠŸΡ€ΠΎΠ³Π»ΡΠ½ΡŒΡ‚Π΅ Ρ‚Π°ΠΊΠΎΠΆ

Attributes overview

Зміст

οΌ‹add a note

User Contributed Notes

There are no user contributed notes for this page.