阅读(4299) (0)

hack属性:介绍

2017-12-01 11:25:24 更新

属性是元数据的任何一个保持器可反射一段代码。这包括函数,类,接口,特性等

除了一些特殊的属性,属性是一个纯粹的句法实体。他们利用工具来完成任务。例如,您可以拥有基于文档的属性,其内容通过反射提取。

<?hh

namespace Hack\UserDocumentation\Attributes\Intro\Examples\Simple;

<<ClassOwner("Joel Marcey"), Description("This class does nothing")>>
class Simple {}

function get_attributes(): void {
  $rc = new \ReflectionClass(
    "Hack\UserDocumentation\Attributes\Intro\Examples\Simple\Simple"
  );
  var_dump($rc->getAttributes()["Description"]);
}

get_attributes();

Output

array(1) {
  [0]=>
  string(23) "This class does nothing"
}

特殊属性

一些属性对Hack类型检查器和HHVM都有特殊的含义。这里是最常见和有用的:

还有一些不太常见的特殊属性