# allowdynamicproperties

# 

<div data-wrapper="1" role="class">

ENTITY

</div>

<div data-wrapper="1" role="class">

This attribute is used to mark classes that allow [dynamic properties](#language.oop5.properties.dynamic-properties).

</div>

> [!NOTE]
> 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.

> [!NOTE]
> This attribute was introduced in PHP 8.2.0 as the explicit way to allow dynamic properties. Creating a dynamic property on a class that does not use this attribute, and that does not implement [\_\_get()](#object.get) or [\_\_set()](#object.set), is deprecated.

# 

<div data-wrapper="1" role="class">

ENTITY

</div>

<div data-wrapper="1" role="attribute">

\#\[\Attribute(\Attribute::TARGET_CLASS)\]

</div>

<div data-wrapper="1" role="class">

final

</div>

<div data-wrapper="1" role="class">

AllowDynamicProperties

</div>

<div data-wrapper="1" role="comment">

ENTITY

</div>

# 

<div data-wrapper="1" role="class">

ENTITY

</div>

<div class="example" role="class">

<div class="title">

AllowDynamicProperties with a dynamic property

</div>

To enable dynamic properties, add `#[\AllowDynamicProperties]`.

```
<?php
#[\AllowDynamicProperties]
class ValidExample { }

$obj = new ValidExample();
$obj->myDynamicProp = 1234;
?>

    
```

</div>

<div class="example" role="class">

<div class="title">

AllowDynamicProperties with non-existing property

</div>

As of PHP 8.2.0, the declaration is required unless the class uses this attribute. Creating an undeclared property on such a class emits a deprecation notice.

```
<?php
class InvalidExample { }

$obj = new InvalidExample();
$obj->myUnknownProperty = 1234;
?>

    
```

ENTITY

    Deprecated: Creation of dynamic property InvalidExample::$myUnknownProperty is deprecated in file on line 5

        

</div>

<div class="example" role="class">

<div class="title">

AllowDynamicProperties with non-existing property in inherited class

</div>

```
     
<?php
class DefaultBehaviour { }

#[\AllowDynamicProperties]
class ClassAllowsDynamicProperties { }

class InheritedClassAllowsDynamicProperties extends ClassAllowsDynamicProperties { }

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

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

    
```

ENTITY

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

        

</div>

# 

<div data-wrapper="1" role="class">

ENTITY

</div>

<div data-wrapper="1" role="class">

[Attributes overview](#language.attributes)

</div>

<div data-wrapper="1" role="class">

ENTITY

</div>