views:

152

answers:

3

Hello there, Type hinting helps the compiler to assume the type of the variable, but, as the PHP is a dynamic scripting interpreted language, the question came to my mind if it's possible that type hinting even makes the runtime faster or not?

+1  A: 

All type hinting in PHP does is add code that tests the type of the parameters and fails if its not what's expected, so type hinting NEVER helps performance. Its only real use is for debugging, so if you have a function that's called very often, removing the type hint in the production code can speed things up, especially since checking the type of an object isn't the fastest thing in the world.

also see when should I use type hinting in PHP? and abusing type hinting in PHP

CrazyJugglerDrummer
Accurate except for "Its only real use is for debugging". Type hinting introduces a level of safety to your code.
Alan Storm
+1  A: 

Type hinting only hinders performance because it requires (for objects) to test the inheritance hierarchy. To make things worse, this kind of check is expensive in PHP because it is done in time proportional to the depth of the hierarchy.

A test for type, such the one done for the array hint is a much smaller overhead, though.

Furthermore, it is currently not used for any optimization purposes.

It is however, a useful defensive programming feature.

Artefacto
+8  A: 

PHP is a dynamic language.
Dynamic languages only support runtime checking.
Type hinting is runtime type checking.
Runtime type checking is bad for performance.
Therefore, type hinting is bad for performance.

SHiNKiROU
+1 - I like this, it's almost like a zen of type-hinting (or something) :)
karim79
What about running Zend Optimizer or something similar with caches the opcode?
MANCHUCK