Re: [RFC] Keep type of reference params
| From: | Manuel Canga | Date: | Sat, 09 May 2020 18:44:03 +0000 |
| Subject: | Re: [RFC] Keep type of reference params | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-110103@lists.php.net to get a copy of this message | ||
---- En vie, 08 may 2020 23:40:22 +0200 Bob Weinand <bobwei9@hotmail.com> escribió ----
> > Am 04.05.2020 um 10:53 schrieb Manuel Canga <php@manuelcanga.dev>:
> >
> > Hi internals,
> >
> >
> >
> > I would like to present a possible new RFC( "keep type of reference params" )
> > for your
> >
> > consideration.
> >
> >
> >
> > Firstly, an example:
> >
> >
> >
> > ```
> >
> > <?php
> >
> >
> >
> > function my_array_shift( array & $array ) {
> >
> > $array = "string";
> >
> > }
> >
> >
> >
> > $array = [ 0, 1, 2, 3, 4 ];
> >
> >
> >
> > my_array_shift($array);
> >
> >
> >
> > count( $array );
> >
> > ```
> >
> >
> >
> > The result of this code is a warning( in count line ) because of $array is a string.
> >
> > However, I think it should be an error or exception when a string is assigned to $array
> > var.
> >
> > In my opinion, $array var would have to keep its type when function ends.
> >
> >
> >
> > What is your opinion ? Do you see it useful ?
> >
> > Thanks and I'm sorry for my English( I'm a Spanish ).
> >
> > Regards
> >
> > --
> >
> > Manuel Canga
>
> Hey Manuel,
>
> the primary issue (apart from the BC break) here is leaking the reference across the function
> boundary.
>
> function a(array &$a) {
> $GLOBALS["globalA"] = &$a;
> }
>
> funcition b() {
> $GLOBALS["globalA"] = 10;
> }
>
> $a = 1;
> a($a);
> b();
> // $a is magically changed to 10
>
> Yes, you can here verify, that $a is an array at the function boundaries, but you cannot
> afterwards.
>
> If we had proper inout parameters (which do not leak a reference, but assign the value of the
> variable (in callee scope) back to the passed variable from caller), then we could easily enforce
> it.
>
> But as it stands now, this is not an option. (Especially due to the false promise this seems
> to make.)
>
> Bob
Thanks Bob, that was a great example
I explain...I am very forgetful, so it's very easy for me to write somethink like this:
```
function filter_something( &string $to_filter) {
$to_filter = strtolower($to_lower);
$to_filter = sanitize_this($to_filter);
.....
}
filter_something($my_string);
```
Sometimes, one of these functions can returns a false|null|numeric|... instead of expected type.
Also, If any function in 'filter_somethid' would throw an exception, value in $to_filter
would finish inconsistent
However, with:
```
function filter_something( inout string $to_filter) {
$to_filter = strtolower($to_lower);
$to_filter = sanitize_this($to_filter);
.....
}
filter_something(inout $my_string);
```
PHP could in the end produce exception when any function in
filter_something modify
type of $my_string or
PHP could keep value of $to_filter as well if any function throwed an exception.
Regards
--
Manuel Canga