<?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.delayedtargetvalidation.php',
    1 => 'DelayedTargetValidation',
    2 => 'The DelayedTargetValidation attribute',
  ),
  'up' => 
  array (
    0 => 'reserved.attributes.php',
    1 => 'Predefined Attributes',
  ),
  'prev' => 
  array (
    0 => 'allowdynamicproperties.construct.php',
    1 => 'AllowDynamicProperties::__construct',
  ),
  'next' => 
  array (
    0 => 'class.deprecated.php',
    1 => 'Deprecated',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/predefined/attributes/delayedtargetvalidation.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.delayedtargetvalidation" class="reference">
 <h1 class="title">The DelayedTargetValidation attribute</h1>
 

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

  <div class="section" id="delayedtargetvalidation.intro">
   <h2 class="title">Introduction</h2>
   <p class="simpara">
    This attribute delays target validation errors for internal attributes
    from compile time to when the attribute is instantiated via the Reflection API.
   </p>
   <p class="simpara">
    When applied to a declaration, any invalid usage of internal attributes
    on the same target will not trigger a compile time error. Instead, the
    validation is deferred and performed when the attribute is instantiated
    via <a href="reflectionattribute.newinstance.php" class="link">ReflectionAttribute::newInstance()</a>.
   </p>
   <p class="simpara">
    This is primarily intended for forward compatibility, allowing code to
    use attributes that may gain additional valid targets in future PHP
    versions without breaking on older versions.
   </p>
  </div>

  <div class="section" id="delayedtargetvalidation.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">DelayedTargetValidation</strong></strong>
     {</div>
   }</div>

  </div>

  <div class="section" id="delayedtargetvalidation.examples">
   <h2 class="title">Examples</h2>

   <div class="example" id="example-1">
    <p><strong>Example #1 Delaying validation of an invalid target</strong></p>
    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Base </span><span style="color: #007700">{<br />    protected function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">void </span><span style="color: #007700">{}<br />}<br /><br />class </span><span style="color: #0000BB">Child </span><span style="color: #007700">extends </span><span style="color: #0000BB">Base </span><span style="color: #007700">{<br /><br />    #[</span><span style="color: #0000BB">\DelayedTargetValidation</span><span style="color: #007700">]<br />    #[</span><span style="color: #0000BB">\Override</span><span style="color: #007700">]<br />    public const </span><span style="color: #0000BB">NAME </span><span style="color: #007700">= </span><span style="color: #DD0000">'child'</span><span style="color: #007700">;<br /><br />    #[</span><span style="color: #0000BB">\Override</span><span style="color: #007700">]<br />    protected function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(): </span><span style="color: #0000BB">void </span><span style="color: #007700">{}<br />}</span></span></code></div></div>


    <div class="example-contents"><p>
     On PHP versions where <span class="classname"><a href="class.override.php" class="classname">Override</a></span> is not allowed on
     class constants, this does not produce a compile time error.
    </p></div>
   </div>

   <div class="example" id="example-2">
    <p><strong>Example #2 Validation occurs during reflection</strong></p>
    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$reflection </span><span style="color: #007700">= new </span><span style="color: #0000BB">ReflectionClassConstant</span><span style="color: #007700">(</span><span style="color: #0000BB">Child</span><span style="color: #007700">::class, </span><span style="color: #DD0000">'NAME'</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$reflection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAttributes</span><span style="color: #007700">() as </span><span style="color: #0000BB">$attribute</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">newInstance</span><span style="color: #007700">(); </span><span style="color: #FF8000">// May throw if invalid<br /></span><span style="color: #007700">}</span></span></code></div></div>


    <div class="example-contents"><p>
     When any attribute applied to the same target (other than
     DelayedTargetValidation itself) is instantiated via reflection using
     <a href="reflectionattribute.newinstance.php" class="link">ReflectionAttribute::newInstance()</a>,
     target validation is performed and an exception may be thrown if the attribute
     is used on an unsupported target.
    </p></div>
   </div>

  </div>

  <div class="section" id="delayedtargetvalidation.notes">
   <h2 class="title">Notes</h2>
   <p class="simpara">
    This attribute only affects target validation of internal attributes.
   </p>
   <p class="simpara">
    It does not suppress functional validation performed by those attributes.
    For example, <span class="classname"><a href="class.override.php" class="classname">Override</a></span> will still emit an error if a
    method does not actually override a parent method.
   </p>
  </div>

  <div class="section" id="delayedtargetvalidation.seealso">
   <h2 class="title">See Also</h2>
   <ul class="simplelist">
    <li><a href="language.attributes.php" class="link">Attributes overview</a></li>
    <li><a href="class.override.php" class="link">Override</a></li>
   </ul>
  </div>

 </div>

</div>
<?php manual_footer($setup); ?>