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

Self As Attribute

By Jan Nedbal

<?php

#[Attribute]
class MyAttribute {
    #[self]
    public function __construct() {}
}

Self, parent and static cannot be used as attributes. They are not recognized, and PHP will search for a class called \self instead, in vain.

On the other hand, it is possible to use an attribute onto its own methods, simply by using its regular name.

It is possible to use these names as attributes, as long as they are not instantiated.

It is also possible to use aliases with the attributes: they will automatically resolve to the original class, not the aliased name.

See Also

PHP Features

Last updated: 10 September 2026