PHP Type Hinting
PHP Type Hinting may soon come to PHP from a patch from Ilia Alshanetsky. This feature has been a long time debate on internals and several RFC’s for type hinting have come about.
You may be wondering why you would want type hinting in a dynamic language. This can come in very handy especially if you are looking to enforce a specific type or need to ensure that you are indeed working with say an integer. Since this is written in C, it will be far faster than actually type casting it yourself or enforcing it yourself. General speed improvements are always nice to have.
Examples
The following example will only accept a type of integer, if it is not an integer it will fail except in the cases of null values.
1 2 3 | function find (int $id) { .... } |
What if you simply would like to type cast the variable and force it to a value of an integer?
1 2 3 | function find ((int) $id) { .... } |
State of the Vote
So far it looks like the vote is going to pass with everyone in favor, remember the day is still young although it has been posted for just about 11 hours as I write this posting. The current vote is 16 in favor, 0 not in favor. While the vote may change as the day goes on, it looks like it will pass with flying colors up to this point. Ilia thank you for the work that you’ve put into this patch having it not affect the binary level to keep compatibility for the current 5.x branches!
Cloud Computing