Is type hinting helping the performance of PHP scripts?

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.


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?


I did a benchmark

https://github.com/EFTEC/php-benchmarks#type-hinting (you can check the whole code, test it and check it by yourself).

/**
 * @param DummyClass $arg1
 * @param DummyClass $arg2
 *
 * @return DummyClass
 */
function php5($arg1,$arg2){
    return new DummyClass();
}
function php7(DummyClass $arg1,DummyClass $arg2): DummyClass {
    return new DummyClass();
}
  • php5 0.0006339550018310547 seconds
  • php7 0.0007991790771484375 seconds

And type hinting is around 10% slower than without type hinting. It is not considerably slow unless it is required in a vital part of the code (a huge loop for example) or we need raw performance.

I prefer PHPdoc, it works similar, it's more complete and it doesn't affect the performance.


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.