't Bijstere spoor

't Bijstere spoor

A blog about Web development

PHP feature request: 'Throwable' interface

I would love to have a 'Throwable' inteface in PHP, so I opened my very first feature request.

I realize this is mostly an OOP purist request, as it won't provide any real functionality.

If you agree, vote!


Comments

Tim
Tim said on Friday, 19 June 2009 at 3:00 am CEST

Heh, one week ago I also thought about it. Would be a nice feature though. :)

Status has been changed to Bogus by Johannes. Therefore voting is not possible anymore. :(

Greg Beaver
Greg Beaver said on Friday, 19 June 2009 at 4:38 am CEST

Why would you need this? Just extend Exception. This is also OOP purism, but the way it was intended.

One of the problems with the PHP exception implementation is that all the methods are final, but this will be remedied in PHP 6, or at least is on the list.

Evert
Evert said on Friday, 19 June 2009 at 4:45 am CEST

Hi Greg,

The problem is that I want to be able to add this 'getHTTPStatus' behaviour to Exception classes already part of an existing hierarchy.

Especially as a library developer, it will allow implementors to customize exception handling, or messaging if they fulfill the interface 'contract'. This applies to any usecase for Interfaces in general, but because the ancestor for every exception is a class, interfaces are not as useful.

It's no big deal, I can just catch any exception, and in the catch-clause I can still check if they implement my interface, but I figured it makes just a slightly uglier implementation.

That being said, Johannes (who closed the bug) had a pretty compelling argument.. It was worth the try though :)

Greg Beaver
Greg Beaver said on Friday, 19 June 2009 at 6:35 am CEST

you can also catch an interface, perhaps this solves your issue?

interface B {}
class C extends Exception implements B {}

try {
throw new C('hi');
} catch (B $e) {
echo "pretty neat\n";
}

Jiri Fornous
Jiri Fornous said on Friday, 19 June 2009 at 9:37 am CEST

I agree with Greg, building exception hierarchy over Exception base class is much more conceptual way. That's why for example PDOException, which was built aside Exception hierarchy, moved over Exception base class.

Christian Fraunholz
Christian Fraunholz said on Friday, 19 June 2009 at 11:47 am CEST

Hi Greg,

I can only offer a PHP script for throwable browsers:
http://blog.thinkphp.de/archives/394-iLevate.html

David Rodger
David Rodger said on Friday, 19 June 2009 at 12:51 pm CEST

@Greg:
try {
throw new C('hi');
} catch (B $e) {
echo "pretty neat\n";
}

This is what Troels Knak-Nielsen does in his konstrukt framework, bootstrapped here:
http://code.google.com/p/konstrukt/source/browse/trunk/lib/konstrukt/konstrukt.inc.php#1491
with an example here:
http://code.google.com/p/konstrukt/source/browse/trunk/lib/konstrukt/konstrukt.inc.php#968

Evert
Evert said on Friday, 19 June 2009 at 5:16 pm CEST

Greg,

That's a great idea actually.. I didn't think of that at all.

Thnx,
Evert

Gerrit Jan van Ahee
Gerrit Jan van Ahee said on Tuesday, 14 July 2009 at 9:13 am CEST

I want an exception that aggregates data and that I can loop over. This is the idea: a ValidationException is thrown after validation of a bunch of data, i.e. an error for each piece of invalid data is to be added to the exception.

It needs to extend Exception in order to be thrown/caught, but it would be very nice if it could "extends ArrayObject implements Throwable." This would save me the time/trouble redirecting most of ArrayObjects functionality to an ArrayObject attribute. (__call is not really a nice solution, I need it for other purposes anyway).

So either a Throwable interface or multiple inheritance would do the trick as far as I can see.







Solve this simple math problem to prevent bots from spamming this blog:
1 + 4 =