RE: [PHP-DEV] [POC - Patch] Scalar Type Hinting - A-La zend_parse_parameters
| From: | John Crenshaw | Date: | Fri, 09 Mar 2012 22:58:19 +0000 |
| Subject: | RE: [PHP-DEV] [POC - Patch] Scalar Type Hinting - A-La zend_parse_parameters | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-58826@lists.php.net to get a copy of this message | ||
From: Simon Schick [mailto:simonsimcity@googlemail.com]
>
> 2012/3/9 Lazare Inepologlou <linepogl@gmail.com>
> >
> > Type casting combined with passing by reference is problematic in many ways. Just an
> > example:
> >
> > fuction foo( string & $buffer) { ... } foo( $my_buffer );
> >
> > Here, $my_buffer has just been declared, so it is null. Should this be an error? I
> > don't know! So, I think that that passing by reference should not be (immediately) supported.
> >
>
> Hi, Lazare
>
> This should at least throw an E_NOTICE :) And also an error as NULL is not allowed here.
>
> Let me modify your example:
>
> fuction foo( string & $buffer = NULL) { ... } foo( $my_buffer );
>
> This would only raise the E_NOTICE because the variable has not been declared.
> Or would you say that NULL is equal with an empty string (talking about implicit casting)? I
> would not like that, but if, don't let it be (int)0 or (bool)false as well.
>
> Bye
> Simon
The reason you have to validate the input type in this case is because even though it is a
reference, we don't ACTALLY know that it isn't supposed to contain an input (even though
that would be against all sane rules most of the time).
I'm not attached to this idea at all, but I thought I'd throw it out and see if anyone can
think of a problem with it; what if we added extra out and inout hints for references?
// So this would give no error at all. Parameter is anticipated to be for output. Just silently
change the type and don't warn on anything.
fuction foo( out string & $buffer) { ... } foo( $my_buffer );
// This WOULD give an error, because the parameter is also an input parameter:
fuction foo( inout string & $buffer = NULL) { ... } foo( $my_buffer );
// In any case no errors on these:
fuction foo( inout string & $buffer = NULL) { ... } foo( (string)$my_buffer );
fuction foo( string & $buffer = NULL) { ... } foo( (string)$my_buffer );
If we assumed that all references were out unless stated otherwise we could avoid reserving an
"out" keyword, and only add "inout", which is unlikely to conflict with stuff.
Like I said, no attachment to this at all. My gut tells me I may have missed something really stupid
here. Just brainstorming.
John Crenshaw
Priacta, Inc.