<?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 => 'zh',
  ),
  '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 读取注解',
  ),
  'next' => 
  array (
    0 => 'language.references.php',
    1 => '引用的解释',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    '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> 声明的空类即可满足需求。可以使用
    <code class="literal">use</code> 语句从全局命名空间导入该注解。
   </p>

  <div class="example" id="example-1">
   <p><strong>示例 #1 简单的 Attribute 类</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">
   要限制注解可以应用的声明类型，可以将位掩码作为第一个参数传递给 <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<br /></span><span style="color: #007700">{<br />}</span></span></code></div>
    </div>


    <div class="example-contents"><p>
     在另一个类型中声明 <span class="classname"><strong class="classname">MyAttribute</strong></span> 会在调用
     <span class="function"><a href="reflectionattribute.newinstance.php" class="function">ReflectionAttribute::newInstance()</a></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> flag 进行指定。
   </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<br /></span><span style="color: #007700">{<br />}</span></span></code></div>
    </div>


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