Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Not In A Closure

Calling Closure native methods outside a closure or an arrow function leads to an error message.

This applies to functions or methods, that are later turned into a closure with the first class callable syntax: while that syntax creates a closure, the underlying code is not a closure, and cannot access the related features.

The warning message, used in previous PHP version, was not as explicit as the new one.

PHP code

<?php

function foo() {
    Closure::getCurrent();
}

foo(); // Error

foo(...)(); // Error: foo was put inside a closure, but it is still not a closure itself.

?>

Before

PHP Fatal error:  Uncaught Error: Call to undefined method Closure::getCurrent()

After

PHP Fatal error:  Uncaught Error: Current function is not a closure

Fatal error: Uncaught Error: Current function is not a closure

PHP version change

This behavior changed in 8.5.

Error Messages

© 2023-2026, Damien Seguy - Exakat