3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set('zend.exception_ignore_args', 1); function main() { try { foo(false); } catch (Exception $ex) { echo $ex->getMessage() . PHP_EOL; } try { foo(true); } catch (Exception $ex) { echo $ex->getMessage() . PHP_EOL; } } function foo($ok) { $file = new SplFileObject('/tmp/hoge.txt', 'a'); if ($file->flock(LOCK_EX|LOCK_NB) == false) { throw new RuntimeException("Unable lock file"); } bar($file, $ok); // この関数を抜けたときにファイルは閉じられるはず } function bar(SplFileObject $file, $ok) { // なにか処理する if ($ok == false) { // 失敗したので例外を投げる throw new RuntimeException("oops!!!"); } echo "ok\n"; } main();

preferences:
25.41 ms | 404 KiB | 5 Q