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
- Original tweet
- self is not supported [Try me]
PHP Features
Last updated: 10 September 2026