PHPUnit Bootstrap with PHP Autoload

I have a scenario wherein after upgrading PHPUnit 3.4.11 to version 3.5.3, an existing bootstrap file for phpunit suddenly encountered an error complaining about class was not found. It seems PHP __autoload is not working.

After few experiments, a solution came up as shown below.

This is the previous method of autoloading class.
if (!function_exists('__autoload')) {

    function __autoload($class) {

        $file = str_replace('_','/',$class.'.php');

        //Suppress errors in case it can't find the file.
        @include_once $file;

    }
}

Now here is the new version that works for PHPUnit 3.5
function autoload($class) {
    $file = str_replace('_','/',$class.'.php');
    try{
        include_once $file;
    } catch (Exception $e) {

    }
}
spl_autoload_register('autoload');

5 comments:

bobs said...

Thanks, had the same problem of the autoload not working.

Maxim said...

thankyou, I had the same problem.

Anonymous said...

What Maxim said - had the same issue. Was poking at it for an hour or two before I stumbled on this post. Thanks!

shishi said...

thanks a lot

seiki said...

thanks a lot