Thursday 25 June 2015

PHP 7: alpha 2 breaks EngineExceptions (on purpose, as it turns out)

I thought I was going mad... the code I wrote for yesterday's article ("PHP 7: PHP starts to play catch-up with error- / exception-handling") wasn't working today. Worried I was leading eveyrone up the garden path I revisited it, and finally worked out what the story is. This morning I "upgraded" PHP to PHP 7/alpha 2. And it broke the code.

Here's a pared-back repro:

// engineException.php

try {
    function takesInt(int $x){
        return true;

    takesInt('not an int');
} catch(EngineException $e) {
    echo 'EngineException caught<br>';

On alpha 1, this results in:

EngineException caught

And that's what it should do.

On Alpha 2, I get this:

Fatal error: Uncaught TypeError: Argument 1 passed to takesInt() must be of the type integer, string given, called in D:\php\php.local\www\experiment\7\exceptionHandling\engineException.php on line 9 and defined in D:\php\php.local\www\experiment\7\exceptionHandling\engineException.php:5 Stack trace: #0 D:\php\php.local\www\experiment\7\exceptionHandling\engineException.php(9): takesInt('not an int') #1 {main} thrown in D:\php\php.local\www\experiment\7\exceptionHandling\engineException.php on line 5

That's unhelpful.

I s'pose I better go find how to raise a bug... (... raised as 69929).


The PHP bods got back to me... they have actively revised how the inheritance hierarchy of Throwables works, and this specific situation was a casuality of that. The given situation is not longer an EngineException, it's a TypeError. If I catch one of those, the code works.

Such is the perils running alpha code, eh?