Re: RFC: Type hinting revisited for PHP 5.3
| From: | Paul Biggar | Date: | Wed, 01 Jul 2009 21:36:46 +0000 |
| Subject: | Re: RFC: Type hinting revisited for PHP 5.3 | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-44611@lists.php.net to get a copy of this message | ||
On Wed, Jul 1, 2009 at 10:23 PM, Hannes
Magnusson<hannes.magnusson@gmail.com> wrote:
> On Wed, Jul 1, 2009 at 23:07, Paul Biggar<paul.biggar@gmail.com> wrote:
>> So, what you're saying is, the patch already handles coercion? If
>> that's the case, then problem solved.
>
> The patch offers scalar type _hinting_. Not type _casting_.
>
> Type hinting in PHP works very simply: If the value doesn't type-match
> the argument information (arginfo internally) then it will be rejected
> and E_RECOVERABLE_ERROR thrown.
>
> In most circumstances that error is fatal. However. If the user
> chooses then he can ignore that error (by creating his own error
> handler) and continue the execution.
>
> Type _hinting_ is in no way related to type _casting_.
It should be.
The current type hinting is for objects, which in PHP are strongly
typed. We plan to extend it to scalars, which in PHP are weakly typed.
Adding a strong type system for scalars goes against the rest of the
language.
As Stas said:
> With this patch, we won't have one logic anymore - we'd have two logics - one for
> typehinted functions (reject everything that doesn't match the type) and one for the rest of
> the language (try to coerce
> types). Two logics in one language is usually not good.
PHP already has 2 type systems. I don't think that adding a 3rd one is
complementary.
> Furthermore, the patch introduces couple of new types, "scalar" and
> "numeric". These are "magic types" and do value-to-real-type
> comparison. No type casting.
> The scalar type hint accepts strings, booleans, ints and floats.
> The numeric type hint accepts strings (that pass is_numeric()),
> booleans, ints and floats.
Yes. Therefore only the scalar and numeric types are useful. Nobody
wants to use an 'int' hint that fails on numeric strings.
Also, I don't know what happens for string hints when you pass an
object with a __toString handler, but it should be allowed.
Thanks,
Paul
--
Paul Biggar
paul.biggar@gmail.com