PHP Type Hinting

by Mike Willbanks on July 7th, 2009

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.


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.

function find (int $id) {

What if you simply would like to type cast the variable and force it to a value of an integer?

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!

From PHP

  1. w00t !
    I hope this feature will be released as soon as possible. The part type hinting was really stupid…

    Thanks for the article :)

  2. mnt permalink

    Another half-cooked idiotic idea. And what about member variables of classes? getter/setter orgies?


    I hate php. I wanna die. Shoot me now.

  3. rsg permalink

    How can people be so against optional functionality?

    Personally I would consider the ‘concrete type hint’ bad form in most cases. It prevents the transparent replacement of $id with an object that provides __toInt(). Wait… where did I put that __toInt() function…. 😉

  4. I think most people want a complete implementation. Like mnt mentions, what about member variables in classes? There will be a disconnect between each individual setter function and the member variable it is assigning to. Also, it does not work for the __get() magic method.

    It is better to wait and get this fleshed out. It worked well for namespaces…

  5. Stephen Beattie permalink

    mnt above is right in saying that type-hinting in PHP is half-baked. We should have return value type hinting from functions and getters/setters s well as for member variables.

    Any progress like this is welcome though

  6. I’m fairly certain this won’t happen in 5.3, so likely wait for the next minor (5.4 or 6.0) release for this type of feature.

  7. Justin Hendrickson permalink

    I noticed they added some new SPL classes to handle basic types. I guess this is going to superceed it though.

  8. A.R. permalink

    The SPL types SplInt, SplString, etc, are rather unsightly.

    What would be nice is some syntactic sugar, e.g.:

    int $x;
    translates to: $x = new SplInt();

    int $x = 123;
    translates to: $x = new SplInt(123);

    function f(int $x) { … }
    translates to: function f(SplInt $x) { … }

    One advantage of type-hinting is that PHP compilers would be able to deliver a huge performance gain (more dramatic than interpreted PHP).

Trackbacks & Pingbacks

  1. Mike Willbanks’ Blog: PHP Type Hinting | Webs Developer

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS