<?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.overview.php',
    1 => 'Aper&ccedil;u des attributs',
    2 => 'Aper&ccedil;u des attributs',
  ),
  'up' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Attributs',
  ),
  'prev' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Attributs',
  ),
  'next' => 
  array (
    0 => 'language.attributes.syntax.php',
    1 => 'Syntaxe des attributs',
  ),
  '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.overview" class="sect1">
   <h2 class="title">Aperçu des attributs</h2>
   <p class="verinfo">(PHP 8)</p>

   <p class="para">
    Les attributs PHP fournissent des métadonnées structurées et lisibles par machine pour les classes, méthodes,
    fonctions, paramètres, propriétés et constantes. Ils peuvent être inspectés à l&#039;exécution
    via l&#039;<a href="book.reflection.php" class="link">API de réflexion</a>, permettant un comportement dynamique
    sans modifier le code. Les attributs offrent un moyen déclaratif d&#039;annoter le code avec des métadonnées.
   </p>
   <p class="para">
    Les attributs permettent de découpler l&#039;implémentation d&#039;une fonctionnalité de son utilisation.
    Alors que les interfaces définissent une structure en imposant des méthodes, les attributs
    fournissent des métadonnées sur plusieurs éléments, y compris les méthodes, fonctions,
    propriétés et constantes. Contrairement aux interfaces, qui imposent l&#039;implémentation de méthodes,
    les attributs annotent le code sans en modifier la structure.
   </p>
   <p class="para">
    Les attributs peuvent compléter ou remplacer des méthodes d&#039;interface optionnelles en fournissant
    des métadonnées plutôt qu&#039;une structure imposée. Prenons une interface <code class="literal">ActionHandler</code>
    qui représente une opération dans une application. Certaines implémentations peuvent nécessiter une
    étape d&#039;initialisation, tandis que d&#039;autres non. Plutôt que d&#039;obliger toutes les classes implémentant
    <code class="literal">ActionHandler</code> à définir une méthode <code class="literal">setUp()</code>, un attribut peut
    indiquer les besoins en initialisation. Cette approche augmente la flexibilité en permettant
    d&#039;appliquer les attributs plusieurs fois si nécessaire.
   </p>

   <div class="example" id="example-1">
    <p><strong>Exemple #1 Implémentation de méthodes optionnelles d&#039;une interface avec des attributs</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">interface </span><span style="color: #0000BB">ActionHandler<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />}<br /><br />#[</span><span style="color: #0000BB">Attribute</span><span style="color: #007700">]<br />class </span><span style="color: #0000BB">SetUp </span><span style="color: #007700">{}<br /><br />class </span><span style="color: #0000BB">CopyFile </span><span style="color: #007700">implements </span><span style="color: #0000BB">ActionHandler<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">string $fileName</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">string $targetDirectory</span><span style="color: #007700">;<br /><br />    #[</span><span style="color: #0000BB">SetUp</span><span style="color: #007700">]<br />    public function </span><span style="color: #0000BB">fileExists</span><span style="color: #007700">()<br />    {<br />        if (!</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fileName</span><span style="color: #007700">)) {<br />            throw new </span><span style="color: #0000BB">RuntimeException</span><span style="color: #007700">(</span><span style="color: #DD0000">"Le fichier n'existe pas."</span><span style="color: #007700">);<br />        }<br />    }<br /><br />    #[</span><span style="color: #0000BB">SetUp</span><span style="color: #007700">]<br />    public function </span><span style="color: #0000BB">targetDirectoryExists</span><span style="color: #007700">()<br />    {<br />        if (!</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory</span><span style="color: #007700">)) {<br />            </span><span style="color: #0000BB">mkdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory</span><span style="color: #007700">);<br />        } elseif (!</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory</span><span style="color: #007700">)) {<br />            throw new </span><span style="color: #0000BB">RuntimeException</span><span style="color: #007700">(</span><span style="color: #DD0000">"Le répertoire cible </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory</span><span style="color: #DD0000"> n'est pas un répertoire."</span><span style="color: #007700">);<br />        }<br />    }<br /><br />    public function </span><span style="color: #0000BB">execute</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">copy</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fileName</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory </span><span style="color: #007700">. </span><span style="color: #DD0000">'/' </span><span style="color: #007700">. </span><span style="color: #0000BB">basename</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fileName</span><span style="color: #007700">));<br />    }<br />}<br /><br />function </span><span style="color: #0000BB">executeAction</span><span style="color: #007700">(</span><span style="color: #0000BB">ActionHandler $actionHandler</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$reflection </span><span style="color: #007700">= new </span><span style="color: #0000BB">ReflectionObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$actionHandler</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">getMethods</span><span style="color: #007700">() as </span><span style="color: #0000BB">$method</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$attributes </span><span style="color: #007700">= </span><span style="color: #0000BB">$method</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAttributes</span><span style="color: #007700">(</span><span style="color: #0000BB">SetUp</span><span style="color: #007700">::class);<br /><br />        if (</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$attributes</span><span style="color: #007700">) &gt; </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />            </span><span style="color: #0000BB">$methodName </span><span style="color: #007700">= </span><span style="color: #0000BB">$method</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">();<br /><br />            </span><span style="color: #0000BB">$actionHandler</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$methodName</span><span style="color: #007700">();<br />        }<br />    }<br /><br />    </span><span style="color: #0000BB">$actionHandler</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">$copyAction </span><span style="color: #007700">= new </span><span style="color: #0000BB">CopyFile</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$copyAction</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fileName </span><span style="color: #007700">= </span><span style="color: #DD0000">"/tmp/foo.jpg"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$copyAction</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory </span><span style="color: #007700">= </span><span style="color: #DD0000">"/home/user"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">executeAction</span><span style="color: #007700">(</span><span style="color: #0000BB">$copyAction</span><span style="color: #007700">);</span></span></code></div>
     </div>

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