Bad class name in the catch statement

Description

A catch clause lists one or more class or interface names (possibly joined with |) that the caught exception must match. Those names have to be resolvable to a concrete class at compile time.

The pseudo-class static refers to the class the method was actually called on, which is only known at runtime, not at compile time. Since PHP needs to know the exact catch types while compiling the try/catch block, static cannot be used there, even though it is otherwise allowed as a return type or inside the method body.

Example

<?php

class Test
{
    public function method()
    {
        try {
            foo();
        } catch (static $e) {
        }
    }
}

?>

Literal Examples

  • Bad class name in the catch statement

Solutions

  • Use the real class name instead of static.

  • Use self if the intent is to catch the class in which the method is defined.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.