‘%s’ is an invalid class name

Description

self describes the current class. It is a relative class name, as it has no literal value.

As such, it doesn’t have an absolute path version, such as \self. Hence, the error emitted there.

This error is also emitted for static and parent.

It was possible to circumvent this check until PHP 8.1, with namespace\%s syntax, where namespace represents the current namespace.

Example

<?php

function foo(\self $a) {};

function foo( $a) : \self {};

class X {
    private \self $f;
    private \x $f2;
}

?>

Literal Examples

  • ‘self’ is an invalid class name

  • ‘parent’ is an invalid class name

  • ‘static’ is an invalid class name

Solutions

  • Remove the initial backslash.

  • Use the literal name of the intended class.