Re: [RFC] Keep type of reference params

From: Date: Fri, 08 May 2020 21:40:22 +0000
Subject: Re: [RFC] Keep type of reference params
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-110095@lists.php.net to get a copy of this message
> 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

« previous php.internals (#110095) next »