PHP 5.1 and Type Hinting

By Deane Barker on November 25, 2005

PHP: Type Hinting: PHP 5.1 was just released and has introduced type hinting. Is this the first step down the road to a more strongly typed language?

PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays

Gadgetopia

Comments

  1. The way I see it, it’s just a convenient way to check for input parameters. You can do that with PHP 4 too, only it requires much more code:

    function doSomething($param1, $param2) {
        if (!is_array($param1)) trigger_error('Error!', E_USER_ERROR);
        if (!is_a($param2, 'MyClass')) trigger_error('Error!', E_USER_ERROR);
        ...
    }
    

    instead of

    function doSomething(array $param1, MyClass $param2) {
        ...
    }
    
  2. I agree that this is an innocent first step, but it’s really the first thing PHP has ever done in terms of typing. I bet future versions will let you type hint for string and numeric as well. How far beyond that is a config switch to strongly type the whole shindig?

Comments are closed. If you have something you really want to say, email editors@gadgetopia.com and we‘ll get it added for you.