4

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
gexicide
  • 38,535
  • 21
  • 92
  • 152

1 Answers1

4

This appears to be a bug in PHP, according to this bug report from April 2014 and this bug report (with links to the fix) from January 2014.

A comment from the person assigned:

As we talked, this is a knew issue, which should be fixed in 5.6+, but due to ABI break, we can not bring the fix to 5.5...

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • So really a bug, shoot. It took me a few hours to figure out and such autoload bombs are everywhere in my code. Guess that is what you get for using a language with immature features... Anyway, thank you for clearing this up! – gexicide Oct 05 '14 at 15:59
  • The issue seems to be fixed in v5.6.1 (See [here](http://php.net/ChangeLog-5.php#5.6.1) – Jeff Lambert Oct 05 '14 at 16:07