<?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 => 'ru',
  ),
  'this' => 
  array (
    0 => 'language.attributes.classes.php',
    1 => 'Объявление классов атрибутов',
    2 => 'Объявление классов атрибутов',
  ),
  'up' => 
  array (
    0 => 'language.attributes.php',
    1 => 'Атрибуты',
  ),
  'prev' => 
  array (
    0 => 'language.attributes.reflection.php',
    1 => 'Чтение атрибутов через API-интерфейс модуля Reflection',
  ),
  'next' => 
  array (
    0 => 'language.references.php',
    1 => 'Объяснение ссылок',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ru',
    '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">Объявление классов атрибутов</h2>

   <p class="para">
    Для каждого атрибута рекомендуют определять отдельный класс. В самом простом случае
    достаточно создать пустой класс и объявить для класса атрибут <code class="literal">#[Attribute]</code>,
    который импортируют из глобального пространства имён оператором use.
   </p>

   <div class="example" id="example-1">
    <p><strong>Пример #1 Пример простого класса с атрибутом</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 </span><span style="color: #007700">{}</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Типы объявлений для нацеливания атрибута ограничивают
    путём передачи битовой маски в первом аргументе объявления
    <code class="literal">#[Attribute]</code>.
   </p>

   <div class="example" id="example-2">
    <p><strong>Пример #2 Спецификация ограничения целей, доступных для присваивания атрибутов</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 </span><span style="color: #007700">{}</span></span></code></div>
    </div>


    <div class="example-contents"><p>
     Вызов метода <span class="function"><a href="reflectionattribute.newinstance.php" class="function">ReflectionAttribute::newInstance()</a></span> теперь выбросит исключение
     при объявлении атрибута <span class="classname"><strong class="classname">MyAttribute</strong></span> для другого типа,
     кроме метода или функции.
    </p></div>
   </div>

   <p class="para">Атрибутам доступны следующие цели:</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">
    По умолчанию атрибут разрешается назначить классу, свойству или другому объекту рефлексии
    только один раз. Назначить одинаковые атрибуты одному объекту рефлексии получится,
    только если объявить атрибут <code class="literal">#[Attribute]</code>
    с флагом <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>Пример #3 
     Пример с константой IS_REPEATABLE, которая разрешит назначать атрибут многократно
    </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 </span><span style="color: #007700">{}</span></span></code></div>
    </div>


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