Debugging closures
Learn how to debug serialized closures
Opis Closure is debugging friendly and can be used in conjunction with error handlers like whoops without any special or supplemental configurations.
use function Opis\Closure\{serialize, unserialize};
// register whoops
$whoops = new \Whoops\Run();
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
$whoops->register();
$closure = function() {
throw new \Exception("You should catch me");
};
// Unserialize a serialized closure
$closure = unserialize(serialize($closure));
// Call the closure to throw the exception
$closure();
Opis Closure allows you to track the residing
source of a serialized closure by using the #trackme
directive inside the closure.
The #trackme
directive provides the following information:
Date
- the datetime when the closure was serialized, represented in the W3C formatFile
- the file where the serialized closure residesLine
- the line where the closure can be located inside the File
use function Opis\Closure\{serialize, unserialize};
$whoops = new \Whoops\Run();
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
$whoops->register();
$closure = function() {
#trackme
throw new \Exception("You should catch me");
};
$closure = unserialize(serialize($closure));
$closure();