Why no type hints for built-in types?

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

 



(I've posted this to the PHP newsgroups, as well, but as many here might not
read them, I post here, as well. I hope that's not considered "overboard",
and if so, please let me know)

Hi.

I'm new here, and sorry if this has been discussed before; I didn't find it
searching the PHP groups. (I've also read recommendations to cross-post to
the other PHP groups, but if that is discouraged, please let me know. At the
same time, please then let me know which of the many PHP groups to post to.
:) )

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 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.

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.

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.

Regards,

Terje

-- 
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