<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'language.attributes.overview.php',
    1 => '&Uuml;bersicht &uuml;ber die Attribute',
    2 => '&Uuml;bersicht &uuml;ber die Attribute',
  ),
  'up' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Attribute',
  ),
  'prev' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Attribute',
  ),
  'next' => 
  array (
    0 => 'language.attributes.syntax.php',
    1 => 'Syntax von Attributen',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    '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">Übersicht über die Attribute</h2>
   <p class="verinfo">(PHP 8)</p>

   <p class="para">
    PHP-Attribute bieten strukturierte, maschinenlesbare Metadaten für Klassen,
    Methoden, Funktionen, Parameter, Eigenschaften und Konstanten. Sie können
    zur Laufzeit über die
    <a href="book.reflection.php" class="link">Reflection-API</a> inspiziert werden und
    ermöglichen dynamisches Verhalten ohne Änderung des Codes. Attribute bieten
    eine deklarative Möglichkeit, den Code mit Metadaten zu kommentieren.
   </p>
   <p class="para">
    Attribute ermöglichen es, die Implementierung eines Merkmals von seiner
    Verwendung zu trennen. Während Schnittstellen die Struktur definieren,
    indem sie Methoden vorschreiben, liefern Attribute Metadaten über mehrere
    Elemente, einschließlich Methoden, Funktionen, Eigenschaften und
    Konstanten. Im Gegensatz zu Schnittstellen, die die Implementierung von
    Methoden vorschreiben, kommentieren Attribute den Code, ohne dessen
    Struktur zu verändern.
   </p>
   <p class="para">
    Attribute können optionale Schnittstellenmethoden ergänzen oder ersetzen,
    indem sie Metadaten anstelle einer vorgeschriebenen Struktur bereitstellen.
    Ein Beispiel könnte eine <code class="literal">ActionHandler</code>-Schnittstelle
    sein, die eine Operation in einer Anwendung repräsentiert. Einige
    Implementierungen könnten einen Konfigurationsschritt erfordern, während
    andere dies nicht tun.
    Anstatt alle Klassen, die <code class="literal">ActionHandler</code> implementieren,
    zu zwingen, eine <code class="literal">setUp()</code>-Methode zu definieren, kann ein
    Attribut die Setup-Anforderungen spezifizieren. Dieser Ansatz erhöht die
    Flexibilität, da Attribute bei Bedarf mehrfach verwendet werden können.
   </p>

   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Implementierung optionaler Methoden einer Schnittstelle mit Hilfe von Attributen</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">"Die Datei existiert nicht"</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">"Das Zielverzeichnis </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory</span><span style="color: #DD0000"> ist kein Verzeichnis"</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); ?>