<?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 => 'es',
  ),
  'this' => 
  array (
    0 => 'language.attributes.overview.php',
    1 => 'Descripci&oacute;n general de atributos',
    2 => 'Descripci&oacute;n general de atributos',
  ),
  'up' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Atributos',
  ),
  'prev' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Atributos',
  ),
  'next' => 
  array (
    0 => 'language.attributes.syntax.php',
    1 => 'Sintaxis de atributos',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'es',
    '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">Descripción general de atributos</h2>
   <p class="verinfo">(PHP 8)</p>

   <p class="para">
    Los atributos de PHP ofrecen metadatos estructurados y legibles por máquinas para clases, métodos,
    funciones, parámetros, propiedades y constantes. Pueden ser inspeccionados en tiempo de ejecución
    a través de la <a href="book.reflection.php" class="link">API de Reflection</a>, lo que permite un
    comportamiento dinámico sin modificar el código. Los atributos proporcionan una forma declarativa de anotar
    el código con metadatos.
   </p>
   <p class="para">
    Los atributos permiten desacoplar la implementación de una funcionalidad de su uso. Mientras que
    las interfaces definen la estructura mediante la imposición de métodos, los atributos proporcionan metadatos en
    varios elementos, incluidos métodos, funciones, propiedades y constantes. A diferencia de las interfaces,
    que obligan a implementar métodos, los atributos anotan el código sin alterar su estructura.
   </p>
   <p class="para">
    Los atributos pueden complementar o reemplazar métodos opcionales de una interfaz al proporcionar metadatos en lugar de
    una estructura impuesta. Considera una interfaz <code class="literal">ActionHandler</code> que representa una
    operación en una aplicación. Algunas implementaciones pueden necesitar un paso de configuración, mientras que otras no.
    En lugar de obligar a todas las clases que implementan <code class="literal">ActionHandler</code> a definir un
    método <code class="literal">setUp()</code>, un atributo puede indicar los requisitos de configuración. Este enfoque
    aumenta la flexibilidad, permitiendo que los atributos se apliquen varias veces cuando sea necesario.
   </p>

   <div class="example" id="example-1">
    <p><strong>Ejemplo #1 Implementación de métodos opcionales de una interfaz mediante atributos</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">"El archivo no existe"</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">"El directorio de destino </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">targetDirectory</span><span style="color: #DD0000"> no es un directorio"</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); ?>