Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

Attributes (#[...])

Reference for PHP 8 attributes: declare metadata with #[...], define your own attribute classes, read them with Reflection and restrict their targets.

Attributes (PHP 8.0) attach structured metadata to classes, methods, functions, properties, constants, parameters and enum cases. They are written as #[Name(arguments)] just before the declaration. PHP does nothing with them by itself. A framework or your own code reads them with Reflection and acts on what it finds.

An attribute is a normal class. The class that declares the attribute is itself marked with #[Attribute]. The arguments are constant expressions, and the attribute object is only created when you call newInstance().

PHP
#[Route("/home", methods: ["GET"])]
function home(): void {}

Try it

PHP Define and read an attribute

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Targets, repetition and raw arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
#[AttributeName]
#[AttributeName(arg1, name: arg2)]
#[First, Second(1)]
Piece Description
#[ ... ] Attribute group. Several attributes can share a group, separated by commas, or use several groups.
Attribute class Declares a class as an attribute. Its optional argument is a bit mask of targets: TARGET_CLASS, TARGET_FUNCTION, TARGET_METHOD, TARGET_PROPERTY, TARGET_CLASS_CONSTANT, TARGET_PARAMETER, TARGET_ALL, plus IS_REPEATABLE.
ReflectionX::getAttributes() Returns ReflectionAttribute objects, optionally filtered by class name.
ReflectionAttribute getName(), getArguments(), getTarget(), isRepeated() and newInstance().

Errors: newInstance() throws Error if the class does not exist, is not marked #[Attribute], is used on the wrong target, or is repeated without IS_REPEATABLE.

Usage notes

  • Validation is lazy. Nothing is checked until newInstance(). Typos in attribute names are silent until then.
  • Constant arguments only. You can pass literals, constants, enum cases and new expressions (PHP 8.1+), not variables or function calls.
  • Comments look similar. Before PHP 8, #[ started a line comment, so older PHP versions ignore the line.
  • Name resolution follows normal namespace rules: import the attribute class with use.
  • Built-in attributes include #[Attribute], #[ReturnTypeWillChange] (8.1), #[AllowDynamicProperties] (8.2), #[SensitiveParameter] (8.2), #[Override] (8.3) and #[Deprecated] (8.4).
  • Prefer attributes over docblock annotations for anything code reads: they are checked by the parser and visible to Reflection.
  • Performance. Reading them through Reflection is not free; frameworks cache the results.