Page 1 of 1

type hint throwing exceptions?

Posted: Sat Oct 04, 2008 4:42 pm
by alex.barylski
Is it possible to have PHP throw an exception when a type hint fails? It seems to only raise an error and ideally I'd like to catch an exception.

I've Googled and found some interesting projects like PHPTypeSafe as well as a similar class on PHP comments but preferably I'd like to just flip a switch or upgrade to a newer version of PHP that supported type hinting with exceptions???

EDIT | I believe I could catch the error by overriding the error reporting method (which I have already done) and throw the exception there...but again I'd prefer if there was a PHP version which just threw exceptions by default. :(

Re: type hint throwing exceptions?

Posted: Sun Oct 05, 2008 12:20 am
by Jenk
Why would you want to catch a type hint exception? It's a developer error, not a production problem..

Re: type hint throwing exceptions?

Posted: Sun Oct 05, 2008 11:20 am
by alex.barylski
Basically I was trying to centralize my error handling, both system and userland.

If I could catch a type hint exception, the idea was, I would email myself with a backtrace. Now I just use trigger_error for things which would (in my case) be categorized as a system error (like testing for existance of a file, instead of relying on the default file not exist error).

I would still like to switch to a all exception approach as it would fit into my architecture best...but because PHP still raises errors I will use errors for system faults and exceptions for userland, just to keep the peace.
Why would you want to catch a type hint exception? It's a developer error, not a production problem..
And why in your opinion should exceptions not be used for this purpose?

Re: type hint throwing exceptions?

Posted: Mon Oct 06, 2008 7:14 am
by Jenk
Because you'll only get that error when you, as a developer, have incorrectly used an interface. It won't be a "sometimes" thing in a production application that can be circumvented, it'll be an ultimate failure.

It's not worth the effort of creating a handler for this type of problem, it's a problem you need to know about immediately. Not something you should log gracefully or some such.