Attributes: Introduction

Attributes are a holder of metadata for any reflectable piece of code. This includes functions, classes, interfaces, traits, etc.

Except for a few special attributes, attributes are a purely syntactic entity. They lend themselves to tooling to accomplish tasks. For example, you can have documentation-based attributes, the contents of which are extracted by reflection.

<?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"
}

Special Attributes

Some attributes have special meaning to both the Hack typechecker and HHVM. Here are the most common and useful ones:

There are a few much less common special attributes as well.