<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc'; $TOC = array(); $TOC_DEPRECATED = array(); $PARENTS = array(); include_once dirname(__FILE__) ."/toc/language.attributes.inc"; $setup = array ( 'home' => array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'it', ), 'this' => array ( 0 => 'language.attributes.syntax.php', 1 => 'Attribute syntax', ), 'up' => array ( 0 => 'language.attributes.php', 1 => 'Attributes', ), 'prev' => array ( 0 => 'language.attributes.overview.php', 1 => 'Attributes overview', ), 'next' => array ( 0 => 'language.attributes.reflection.php', 1 => 'Reading Attributes with the Reflection API', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'language/attributes.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?> <div id="language.attributes.syntax" class="sect1"> <h2 class="title">Attribute syntax</h2> <p class="para"> Attribute syntax consists of several key components. An attribute declaration starts with <code class="literal">#[</code> and ends with <code class="literal">]</code>. Inside, one or more attributes can be listed, separated by commas. The attribute name can be unqualified, qualified, or fully-qualified, as described in <a href="language.namespaces.basics.php" class="link">Using Namespaces Basics</a>. Arguments to the attribute are optional and enclosed in parentheses <code class="literal">()</code>. Arguments can only be literal values or constant expressions. Both positional and named argument syntax are supported. </p> <p class="para"> Attribute names and their arguments are resolved to a class, and the arguments are passed to its constructor when an instance of the attribute is requested through the Reflection API. Therefore, it is recommended to introduce a class for each attribute. </p> <div class="example" id="example-396"> <p><strong>Example #1 Attribute Syntax</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// a.php<br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">MyExample</span><span style="color: #007700">;<br /><br />use </span><span style="color: #0000BB">Attribute</span><span style="color: #007700">;<br /><br />#[</span><span style="color: #0000BB">Attribute</span><span style="color: #007700">]<br />class </span><span style="color: #0000BB">MyAttribute<br /></span><span style="color: #007700">{<br /> const </span><span style="color: #0000BB">VALUE </span><span style="color: #007700">= </span><span style="color: #DD0000">'value'</span><span style="color: #007700">;<br /><br /> private </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br /><br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">value </span><span style="color: #007700">= </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #FF8000">// b.php<br /><br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Another</span><span style="color: #007700">;<br /><br />use </span><span style="color: #0000BB">MyExample\MyAttribute</span><span style="color: #007700">;<br /><br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">]<br />#[</span><span style="color: #0000BB">\MyExample\MyAttribute</span><span style="color: #007700">]<br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">1234</span><span style="color: #007700">)]<br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">value</span><span style="color: #007700">: </span><span style="color: #0000BB">1234</span><span style="color: #007700">)]<br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">::</span><span style="color: #0000BB">VALUE</span><span style="color: #007700">)]<br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"key" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"value"</span><span style="color: #007700">))]<br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">100 </span><span style="color: #007700">+ </span><span style="color: #0000BB">200</span><span style="color: #007700">)]<br />class </span><span style="color: #0000BB">Thing<br /></span><span style="color: #007700">{<br />}<br /><br />#[</span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">1234</span><span style="color: #007700">), </span><span style="color: #0000BB">MyAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">5678</span><span style="color: #007700">)]<br />class </span><span style="color: #0000BB">AnotherThing<br /></span><span style="color: #007700">{<br />}</span></span></code></div> </div> </div> </div><?php manual_footer($setup); ?>