<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.attributes.classes.php',
    1 => 'D&eacute;claration des classes d\'attributs',
    2 => 'D&eacute;claration des classes d\'attributs',
  ),
  'up' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Attributs',
  ),
  'prev' => 
  array (
    0 => 'language.attributes.reflection.php',
    1 => 'Lecture des attributs avec l\'API de Reflection',
  ),
  'next' => 
  array (
    0 => 'language.references.php',
    1 => 'Les r&eacute;f&eacute;rences',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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.classes" class="sect1">
   <h2 class="title">Déclaration des classes d&#039;attributs</h2>

   <p class="para">
    Il est recommandé de définir une classe distincte pour chaque attribut. Dans le cas le plus simple,
    une classe vide avec la déclaration <code class="literal">#[Attribute]</code> est suffisante.
    L&#039;attribut peut être importé depuis le namespace global à l&#039;aide d&#039;une instruction <code class="literal">use</code>.
   </p>

  <div class="example" id="example-1">
   <p><strong>Exemple #1 Classe d&#039;attribut simple</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">namespace </span><span style="color: #0000BB">Example</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 />}</span></span></code></div>
   </div>

  </div>

  <p class="para">
   Pour restreindre les types de déclarations auxquels un attribut peut être appliqué,
   il convient de passer un masque de bits en premier argument de la déclaration <code class="literal">#[Attribute]</code>.
  </p>

  <div class="example" id="example-2">
   <p><strong>Exemple #2 Utilisation de la spécification de la cible pour restreindre l&#039;utilisation des attributs</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">namespace </span><span style="color: #0000BB">Example</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">(</span><span style="color: #0000BB">Attribute</span><span style="color: #007700">::</span><span style="color: #0000BB">TARGET_METHOD </span><span style="color: #007700">| </span><span style="color: #0000BB">Attribute</span><span style="color: #007700">::</span><span style="color: #0000BB">TARGET_FUNCTION</span><span style="color: #007700">)]<br />class </span><span style="color: #0000BB">MyAttribute<br /></span><span style="color: #007700">{<br />}</span></span></code></div>
    </div>


    <div class="example-contents"><p>
     La déclaration de <span class="classname"><strong class="classname">MyAttribute</strong></span> sur un autre type lèvera désormais une exception lors de
     l&#039;appel à <span class="function"><a href="reflectionattribute.newinstance.php" class="function">ReflectionAttribute::newInstance()</a></span>
    </p></div>
   </div>

   <p class="para">Les cibles suivantes peuvent être spécifiées:</p>

   <ul class="simplelist">
    <li><strong><code><a href="class.attribute.php#attribute.constants.target-class">Attribute::TARGET_CLASS</a></code></strong></li>
    <li><strong><code><a href="class.attribute.php#attribute.constants.target-function">Attribute::TARGET_FUNCTION</a></code></strong></li>
    <li><strong><code><a href="class.attribute.php#attribute.constants.target-method">Attribute::TARGET_METHOD</a></code></strong></li>
    <li><strong><code><a href="class.attribute.php#attribute.constants.target-property">Attribute::TARGET_PROPERTY</a></code></strong></li>
    <li><strong><code><a href="class.attribute.php#attribute.constants.target-class-constant">Attribute::TARGET_CLASS_CONSTANT</a></code></strong></li>
    <li><strong><code><a href="class.attribute.php#attribute.constants.target-parameter">Attribute::TARGET_PARAMETER</a></code></strong></li>
    <li><strong><code><a href="class.attribute.php#attribute.constants.target-all">Attribute::TARGET_ALL</a></code></strong></li>
   </ul>

   <p class="para">
    Par défaut, un attribut ne peut être utilisé qu&#039;une seule fois par déclaration.
    Pour autoriser un attribut à être répété, il faut le spécifier dans le masque de bits
    de la déclaration <code class="literal">#[Attribute]</code> en utilisant le drapeau
    <strong><code><a href="class.attribute.php#attribute.constants.is-repeatable">Attribute::IS_REPEATABLE</a></code></strong>.
   </p>

   <div class="example" id="example-3">
    <p><strong>Exemple #3 Utilisation de IS_REPEATABLE pour permettre à un attribut d&#039;être utilisé plusieurs fois dans une déclaration</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">namespace </span><span style="color: #0000BB">Example</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">(</span><span style="color: #0000BB">Attribute</span><span style="color: #007700">::</span><span style="color: #0000BB">TARGET_METHOD </span><span style="color: #007700">| </span><span style="color: #0000BB">Attribute</span><span style="color: #007700">::</span><span style="color: #0000BB">TARGET_FUNCTION </span><span style="color: #007700">| </span><span style="color: #0000BB">Attribute</span><span style="color: #007700">::</span><span style="color: #0000BB">IS_REPEATABLE</span><span style="color: #007700">)]<br />class </span><span style="color: #0000BB">MyAttribute<br /></span><span style="color: #007700">{<br />}</span></span></code></div>
    </div>


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