PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Jani Hartikainen's Blog: PHP typehinting gotcha in exceptions/methods

Note: This article was originally published at PHPDeveloper on 15 July 2011.
PHPDeveloper

Jani Hartikainen has a new post with a gotcha he came across when using typehinting in exceptions and methods.

A small gotcha I recently ran into: If you use typehinting, PHP never checks that the class your typehint specifies actually exists!

He includes simple examples that define the class type hint as some random string and PHP never throws an error for it. He also notes that it's even worse when dealing with namespaces - how it handles exceptions locally unless you specify otherwise. Of course, these "features" can be put to good use in the right situations, but they can be confusing for a developer without a keen eye.