More Attribute Usage¶
<?php
#[MyAttribute]
class x {}
#[MyAttribute]
enum e {}
#[MyAttribute]
interface i {}
#[MyAttribute]
trait t {}
// ===================
#[MyFunctionAttribute]
function foo() {}
$c = #[MyFunctionAttribute] function () {};
$f = #[MyFunctionAttribute] fn () => 1;
PHP attributes are for classes, but also for enums, traits and interfaces. With ::TARGET_CLASS.
PHP attributes are for functions, and also for closures and arrow functions (but not for methods). With ::TARGET_FUNCTION.
Beware of the position of the attribute with closures and arrow functions, though.
See Also¶
PHP Features¶
Last updated: 14 July 2026