<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/reserved.attributes.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'class.allowdynamicproperties.php',
    1 => 'AllowDynamicProperties',
    2 => 'The AllowDynamicProperties attribute',
  ),
  'up' => 
  array (
    0 => 'reserved.attributes.php',
    1 => 'Predefined Attributes',
  ),
  'prev' => 
  array (
    0 => 'attribute.construct.php',
    1 => 'Attribute::__construct',
  ),
  'next' => 
  array (
    0 => 'allowdynamicproperties.construct.php',
    1 => 'AllowDynamicProperties::__construct',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/predefined/attributes/allowdynamicproperties.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.allowdynamicproperties.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.allowdynamicproperties" class="reference">
 <h1 class="title">The AllowDynamicProperties attribute</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 8 &gt;= 8.2.0)</p>

  <div class="section" id="allowdynamicproperties.intro">
   <h2 class="title">Introduction</h2>
   <p class="simpara">
    This attribute is used to mark classes that allow
    <a href="language.oop5.properties.php#language.oop5.properties.dynamic-properties" class="link">dynamic properties</a>.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Although attributes themselves are not inherited, the effect of the
     <code class="literal">AllowDynamicProperties</code> attribute <em>is</em>
     inherited. Child classes of a class marked with this attribute will also
     allow dynamic properties, even if they do not explicitly declare the
     attribute.
    </span>
   </p></blockquote>
  </div>

  <div class="section" id="allowdynamicproperties.synopsis">
   <h2 class="title">Class synopsis</h2>

   <div class="classsynopsis"><div class="classsynopsisinfo">
    
     <span class="attribute"><a href="class.attribute.php">#[\Attribute]</a> </span><br>
     <span class="modifier">final</span>
     <span class="modifier">class</span> <strong class="classname"><strong class="classname">AllowDynamicProperties</strong></strong>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="allowdynamicproperties.construct.php" class="methodname">__construct</a></span>()</div>

   }</div>

  </div>

  <div class="section">
   <h2 class="title">Examples</h2>
   <p class="simpara">
    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.
   </p>
   <div class="example" id="example-1">
    <p><strong>Example #1 AllowDynamicProperties with non-existing property</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">DefaultBehaviour </span><span style="color: #007700">{ }<br /><br />#[</span><span style="color: #0000BB">\AllowDynamicProperties</span><span style="color: #007700">]<br />class </span><span style="color: #0000BB">ClassAllowsDynamicProperties </span><span style="color: #007700">{ }<br /><br /></span><span style="color: #0000BB">$o1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">DefaultBehaviour</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$o2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">ClassAllowsDynamicProperties</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$o1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nonExistingProp </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o2</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nonExistingProp </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Output of the above example in PHP 8.2:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 10
</pre></div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 AllowDynamicProperties with non-existing property in inherited class</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">DefaultBehaviour </span><span style="color: #007700">{ }<br /><br />#[</span><span style="color: #0000BB">\AllowDynamicProperties</span><span style="color: #007700">]<br />class </span><span style="color: #0000BB">ClassAllowsDynamicProperties </span><span style="color: #007700">{ }<br /><br />class </span><span style="color: #0000BB">InheritedClassAllowsDynamicProperties </span><span style="color: #007700">extends </span><span style="color: #0000BB">ClassAllowsDynamicProperties </span><span style="color: #007700">{ }<br /><br /></span><span style="color: #0000BB">$o1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">DefaultBehaviour</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$o2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">InheritedClassAllowsDynamicProperties</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$o1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nonExistingProp </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o2</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nonExistingProp </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Output of the above example in PHP 8.2:</p></div>
    <div class="example-contents screen">
     <div class="cdata"><pre>
Deprecated: Creation of dynamic property DefaultBehaviour::$nonExistingProp is deprecated in file on line 12
</pre></div>
    </div>
   </div>
  </div>

  <div class="section" id="allowdynamicproperties.seealso">
   <h2 class="title">See Also</h2>
   <p class="simpara"><a href="language.attributes.php" class="link">Attributes overview</a></p>
  </div>

 </div>

 


 


<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="allowdynamicproperties.construct.php">AllowDynamicProperties::__construct</a> — Construct a new AllowDynamicProperties attribute instance</li></ul>
</div>
<?php manual_footer($setup); ?>