Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Plus d’usages des attributs

<?php

#[MyAttribute]
class x {}

#[MyAttribute]
enum e {}

#[MyAttribute]
interface i {}

#[MyAttribute]
trait t {}

// ===================

#[MyFunctionAttribute]
function foo() {}

$c = #[MyFunctionAttribute] function () {};

$f = #[MyFunctionAttribute] fn () => 1;

Les attributs PHP sont destinés aux classes, mais aussi aux enums, aux traits et aux interfaces. Avec ::TARGET_CLASS.

Les attributs PHP sont destinés aux fonctions, et aussi aux closures et aux fonctions fléchées (mais pas aux méthodes). Avec ::TARGET_FUNCTION.

Attention à la position de l’attribut avec les closures et les fonctions fléchées, cependant.

See Also

PHP Features

Last updated: 18 September 2026