subscribe

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!</p>

Web mentions

Comments

  • Tim

    Tim

    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

    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

    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

    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

    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

    Hi Greg,

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

    David Rodger

    @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

    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

    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.