Re: Why no type hints for built-in types?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Terje Slettebø <tslettebo@xxxxxxxxxxxx>:
> In PHP5, you can provide "type hints" for functions, like this:
>
> class Person {...}
>
> function f(Person $p)
> {
>   ...
> }
>
> Since this is optional static typing for objects, why not make the same
> capability available for all types, built-in types included?
>
> I come from a background with generally static and strong typing (C++,
> Java), and having worked with PHP a couple of years, I've quite a few times
> got bitten by stupid bugs that could have been caught by static typing, such
> as passing an empty string - which gets converted to 0 in an arithmetic
> context, when the function was supposed to receive a number, or some such,
> and no error is reported. These bugs can be hard to find.

This is where the === and !== comparison operators can come in handy, as
they compare not only the values but the types. I often need to do this
when checking for zeroes.

> This has been suggested in a few Q & A's at Zend, such as this one:
> http://www.zend.com/expert_qa/qas.php?id=104&single=1
>
> --- Start quote ---
>
> Will be a support for type hints of simple types, like
> class Foo{
>     public function bar(int $var) {    }
> }
>
>     No, type hints of simple types will not be supported. The reason is
> PHP's dynamic nature. A number posted to a script will arrive as a string
> even though it's a number. In this case, PHP assumes that "10" and 10 are
> the same thing. Having such type hints would not fit into this
> auto-conversion of PHP.
>
> --- End quote ---
>
> I don't find this answer satisfactory. Yes, PHP has loose/weak typing, but
> at any one time, a value or a variable has a distinct type. In the example
> in the quote above, you'd have to ensure that the value you pass is of the
> right type.

I can recognize that this answer would not be satisfactory for someone
with a background in traditional application architecture. However, PHP
has been developed from the beginning as a programming language for the
web. Since the nature of web requests is to transfer all values as
strings, PHP needs to be able to compare items of different types -- '0'
needs to evaluate to the same thing as 0. This may not be optimal for
many applications, but for most web applications to which PHP is
applied, it is considered a *feature*.

> This would also open the door to overloading, although it seems from the
> replies from Andi and Zeev in the Zend forums that neither optional static
> typing, nor overloading is considered at this time, and likely not in the
> future, either. :/ What I have seen of arguments against it, I haven't found
> sufficiently convincing, so therefore I'd like to hear about the pros and
> cons of optional static typing, and possibly overloading (however, that
> should really be a separate thread). What the PHP manual calls "overloading"
> has really nothing to do with the concept of overloading in other OO
> languages, such as C++/Java.

PHP already supports overloading as you're accustomed to it -- the
syntax is different, and PHP refers to the practice as "variable-lentgh
argument lists". You use func_num_args(), func_get_args(), and
func_get_arg() to accomplish it:

function someOverloadedFun()
{
    $numargs = func_num_args();
    $args    = func_get_args();
    if (0 == $numargs) {
        return "ERROR!";
    }
    if (1 == $numargs) {
        if (is_string($args[0])) {
            return "Received string: $args[0]";
        } elseif (is_object($args[0])) {
            return "Received object!";
        }
    } elseif ((2 == $numargs)) {
        return "Received arg0 == $args[0] and arg1 == $args[1]";
    }
    // etc.
}

Yes, this is more cumbersome than providing hints -- but typically, if I
design properly, I'm checking types within already, and I've already
determined a way to limit what is passed to the function/method. One
technique I use is to pass a single associate array to a function or
method, and grab my arguments from that. 

> There's a rather lively discussion about adding optional static typing in
> Python (http://www.artima.com/weblogs/viewpost.jsp?thread=85551), and unless
> it has already been, maybe it's time for us to consider it for PHP, as well.
> The current static type checking in PHP5 is something rather half-baked,
> only covering user-defined types.

I can definitely see a use for this -- but, again, PHP has been designed
with loose typing as a *feature*. While type hinting may be a nice
additional feature, I have my doubts as to the necessity or overhead it
would incur.

-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux