I have a problem with finally
blocks and autoload. I am using PHP 5.5.9. Here is a minimal example:
<?php
function loadMyClass($class){
include_once $class . '.php';
}
spl_autoload_register("loadMyClass");
try {
try {
// Test::printIt("before "); <--- (1) Commented for now
throw new Exception();
}
finally {
echo "finally1 "
Test::printIt("finally2 ");
}
}
catch (Exception $e) {
Test::printIt("catch ");
}
Test.php:
<?php
class Test {
public static function printIt($i) { echo $i; }
}
As you see, we thrown an exception, and want a finally
and a catch
block to run. Test::printIt
is a simple method that calls echo
on its parameter. The class Test
is loaded via a the registered autoload function. Running this program prints finally1 catch
instead of finally1 finally2 catch
, so the call to Test::printIt(1);
seems to abort the finally block. The problem seems to be the autoload in the finally
block. If we uncomment the line (1), then Test
is already loaded in the finally block. In this case before finally1 finally2 catch
is echoed correctly.
What is the problem here? Is this a bug in PHP? If yes, in which version is it fixed? Or is it my fault because autoloading is supposed to fail in finally blocks?
This is my exact PHP version as output by php -v
:
PHP 5.5.9-1ubuntu4 (cli) (built: Apr 9 2014 17:11:57)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies