The AllowDynamicProperties class

(PHP 8 >= 8.2.0)

Introduction

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

Class synopsis

final class AllowDynamicProperties {
}

Examples

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.

<?php
class DefaultBehaviour { }

#[AllowDynamicProperties]
class ClassAllowsDynamicProperties { }

$o1 = new DefaultBehaviour();
$o2 = new ClassAllowsDynamicProperties();

$o1->nonExistingProp true;
$o2->nonExistingProp true;
?>

Output of the above example in PHP 8.2:

Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 10
add a note

User Contributed Notes 1 note

up
1
josh at joshhanley dot com dot au
1 month ago
If you are using #[AllowDynamicProperties] on a class that is namespaced, you need add a backslash #[\AllowDynamicProperties] to use its fully qualified name or import it.

Fully qualified name example:
<?php
namespace App;

#[\AllowDynamicProperties]
class Post
{
   
__construct()
    {
       
$this->name = "Dynamic Property";
    }
}
?>

Import example:
<?php
namespace App;

use
AllowDynamicProperties

#[AllowDynamicProperties]
class Post
{
   
__construct()
    {
       
$this->name = "Dynamic Property";
    }
}
?>
To Top