parent In Function¶
<?php
class W {}
class X extends W {}
$x = new X;
$f = function () { return parent::class; };
print $f->call($x);
//function foo(parent $p) { return $this; }
?>
parent shall only be inside a class, as it refers to the parent class. This does not happen with a function, so the second line in this example produces a Fatal Error.
It does not produce the same error with a Closure, or Arrow function. This is valid code because it is possible to change the underlying object of the closure, and give it a valid class to run it with. This is done with the Closure::call() and Closure::bindTo() methods.
The main problem, in this situation, is that such code is invalid a writing location, and becomes valid later, after some reconfiguration. Sometimes, static analysis finds it hard to track.
See Also¶
PHP Error Messages¶
PHP Features¶
Last updated: 14 July 2026