Re: Type hinting misunderstood
| From: | Sven Drieling | Date: | Sun, 06 Jan 2008 14:30:19 +0000 |
| Subject: | Re: Type hinting misunderstood | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-34528@lists.php.net to get a copy of this message | ||
Am Sonntag, 6. Januar 2008 12:28 schrieb Alain Williams:
Hello,
> You are confusing the TYPE and the VALUE.
>
> What type hinting means is:
>
> * is the TYPE correct ? If so succeed.
>
> * can the VALUE be 100% converted to the desired TYPE (eg '5' to int) ?
> If so succeed.
>
> * fail
<?php
echo (int) "1", "<br>\n"; // => 1
echo (int) "1a", "<br>\n"; // => 1
echo (int) "1.2", "<br>\n"; // => 1
echo (int) "a1", "<br>\n"; // => 0
?>
Which cases should be allowd for?
function foo(int $bar);
IMHO only
foo("1");
and maybe also
foo("1.0");
But then the type conversion for type hinting is different from
the type conversion in all other cases which makes PHP
probably harder to learn.
IMHO a system that makes sure that a variable must contain
only specific values would be more useful.
$int_number: int;
$float_number: float;
$range: int 1..12; // month
$email_address: emailaddress;
$options: options['foo', 'bar', 'alice', 'bob'];
The problem is still should "1a" convert and assigned to $int_number? But
because value checking is a different context than type hinting this is
maybe easier to learn.
tschuess
[|8:) http://www.sven-drieling.de/