type hint throwing exceptions?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

type hint throwing exceptions?

Post 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. :(
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: type hint throwing exceptions?

Post by Jenk »

Why would you want to catch a type hint exception? It's a developer error, not a production problem..
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: type hint throwing exceptions?

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: type hint throwing exceptions?

Post 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.
Post Reply