PHP 8.4 improves Closure Naming for simplified debugging

In applications that use closures excessively, understanding stack traces as part of the debugging experience has historically been complicated by the fact that the names of closures did not include the source location.

In a PHP stack trace, whenever a frame was represented by a closure, it only contained the reference {closure} and the namespace the closure was declared in, leading to all closures within a namespace looking identical.

Take this line of code, acting as a sort of wrapper trampoline for measuring execution time of a block of code in Shopware 6:

Before PHP 8.4, an exception thrown somewhere in “render” produced the following stack trace:

When the code in StorefrontController::renderStorefront used multiple closures, this stack trace would not uniquely identify which closure was called.

With PHP 8.4, the closure name now contains the source location it was declared in:

Additionally, var_dump of a closure now includes its location:

Tideways Profiler and Xdebug already had code like this, and my colleague Tim contributed it back to PHP core for inclusion in 8.4.

This change will improve Tideways and other APMs/Exception Tracking tools in places where they use PHP backtrace functionality. Just like in Tideways exception tracking here:

Closure naming simplified debugging

An AI would never be able to dive this deep into technical topics surrounding PHP performance! Follow us on LinkedIn or X and subscribe to our newsletter to get the latest posts.

Let’s dive in and find out what is causing performance bottlenecks! Without Tideways, you’re likely to fish in murky waters attempting to figure it out. Try our free trial today and be enlightened.

Benjamin Benjamin 13.11.2024