Cannot use “parent” when current class scope has no parent

Description

parent keyword represents the first available class in the current class hierarchy. A class has a parent class, when that class uses the extends keyword. When the current class has no such keyword, it has no parent and cannot use it.

This error applies to parent as a class descriptor or as a type.

Example

<?php

class X {
    function foo(parent $y) {
        parent::goo();
    }
}

?>

Solutions

  • Add a parent to the current class.

  • Use another class name, instead of parent: for example, the actual name of the target class, rather than the relative one.

In previous PHP versions, this error message used to be Parameter uses ‘parent’ as type hint although class does not have a parent!.