Re: Type hinting misunderstood
| From: | Sam Barrow | Date: | Mon, 07 Jan 2008 04:07:06 +0000 |
| Subject: | Re: Type hinting misunderstood | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-34570@lists.php.net to get a copy of this message | ||
On Sun, 2008-01-06 at 20:02 +0000, Mikko Koppanen wrote:
> >
> > Why I mean by:
> > Type HINTING is not type ENFORCEMENT.
> > is that:
> > function foo(int $a) {}
> >
> > foo(1); // OK
> >
> > foo("1"); // OK - the string is juggled to an int when the
> > function is called
> > // ENFORCEMENT would have (in some interpretations)
> > caused this to
> > // FAIL since the function argument was a string.
> >
>
>
> I have not been following very closely this conversation so this might have
> been answered already:
>
>
> $b = '5';
>
> function foo( int &$a )
> {
> echo gettype( $a );
> }
>
> foo( $b );
>
> echo gettype( $b );
>
>
> what is type of $b after the function call?
>
It won't get there. By calling foo with a string '5', an error will be
thrown.