Allow Dynamic Properties¶
This is a native PHP attribute, which tells the engine that a class can dynamically create properties without declaring them.
This attribute may be added to any class. It is not necessary with stdClass
.
The attributes allow any dynamic property: there is no way to restrict this to a set of names or a number without other means.
<?php
class MyClass {
function foo() {
// creation of a property, without prior definition
// This yields an error
$this->p = 1;
}
}
#[AllowDynamicProperty]
class MyOtherClass {
function foo() {
// creation of a property, without prior definition
// This doesn't yields an error
$this->p = 1;
}
}
class MyThirdClass extends Stdclass {
function foo() {
// creation of a property, without prior definition
// This doesn't yields an error, yet no attribute
$this->p = 1;
}
}
?>
See also No More Dynamic Properties in PHP 9., I hate the deprecation of dynamic properties.
Related : PHP Native Attribute
Added in PHP 8.2+