PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Slawek Lukasiewicz's Blog: Throwing Exception with Type Hinting Failed

Note: This article was originally published at PHPDeveloper on 11 August 2011.
PHPDeveloper

Slawek Lukasiewicz has a helpful hint posted to his blog today about handling type hinting failures in a what he sees as a "more correct" way than just throwing an error - throwing an exception when the hinting criteria's not met.

One of the most annoying behavior of PHP type hinting feature is triggering error instead of throwing exception when instance type is invalid.

By default, a failure generates a "catchable error" that, thanks to the custom error handling methods PHP includes, can be correctly handled. He includes a snippet of code that defines the errorHandler method that checks the error type for E_RECOVERABLE_ERROR and throws the exception (an ErrorException) if found.