3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Lock { function __destruct() { echo "***lock released now***\n"; } } class DummyFileHandler { function lock(string $lock_type): Lock { return new Lock; } function readAll() { throw new DummyFileHandlerException('Uh oh!'); } } class DummyFileHandlerException extends Exception { }; function process_file(string $path): string { try { $file = new DummyFileHandler($path); $lock = $file->lock('shared'); $content = (function (Lock $lock) use ($file) { $local_lock = $lock; unset($lock); return $file->readAll(); })($lock); } catch (DummyFileHandlerException $e) { // release $file and $lock echo "unsetting \$lock...\n"; unset($file, $lock); // but $lock is in fact not yet released, it can be retrieved through the stack trace echo "lock: "; var_dump($e->getTrace()[1]['args'][0]); throw new Exception('Processing failed', previous: $e); } return $content; } process_file('/some/file.txt');

preferences:
40.31 ms | 404 KiB | 5 Q