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

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

PHP Features

Last updated: 10 September 2026