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

Attribute

Attributes are machine readable options that may be added PHP code. Since PHP 8.0, they are accessible from PHP.

Attributes provide extra and customisable options for the PHP engine, static analysis tools and coders alike.

Attributes are a modern version of the phpdoc comment blocks.

Attributes are backward compatible: they can be used in any PHP version, and are ignored when not supported.

Attributes may or may not have a supporting class: attributes may be used for their semantic value, or with related code.

<?php

    #[Attribute]
    class X {} // this class is an Attributes
    
    #[X]  // This attribute may be instanciated, and executed
    #[Z]  // This attribute serves only as a flag. No class is available.
    class Y {} // this class is an Attributes

?>

Documentation

See Also