Cannot Use "parent" in Orphaned Class

Use of the parent keyword in classes that don't have a parent used to result in a fatal error at runtime:

class MyClass
{
    public function method() {
        return parent::$property;
    }
}
$myClass = new MyClass;
$myClass->method();

However, the parent keyword is now forbidden in that context at compile-time instead of runtime. Its mere presence in an orphaned class is deprecated as of PHP 7.4.0, and emits a fatal error as of PHP 8.0.0:

class MyClass
{
    public function method() {
        return parent::$property;
    }
}

See execution result.

Solution

The execution of the invalid code would cause an error since PHP 5.0.0. It's therefore safe to assume that the associated method was never called, or was always resulting in a fatal error, in which case you will need to manually review that code.

If we can confirm that the problematic method is indeed unused, we can remove it. The safest way to remove it would be to clear the body of the method:

class MyClass
{
    public function method() {
    }
}

The above code will work even if there is a return type declaration, since these throw TypeError only when the method is executed:

class MyClass
{
    public function method(): string {
    }
}

Keep The Method

It's not advisable to remove the method itself, because that might cause other errors. For example, the method might be required by an interface or expected by code that analyzes method names, such as via get_class_methods() or the reflection API.

Errors or Warnings

See Also