Class Is A Valid Type
<?php
class foo {
public function barbar(\class $a) {}
// alright, not hing to see
public function bar (\self $a) {}
//Fatal error: '\self' is an invalid class name
}
?>
PHP allows to use \class as a class name in a type. This is also the case for interfaces, enums, traits… and it is also valid for some others keywords.
On the other hand, a good number of PHP keywords are forbidden, and yield an error : \self is an invalid class name.
Of course, once this is typed like that, it is not possible to satisfy the type specification, and such code is actually not usable.
See Also
- Can type \class, cannot type self [Try me]
PHP Features
Last updated: 10 September 2026