Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110103 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11940 invoked from network); 9 May 2020 20:08:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 May 2020 20:08:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E3A601804B4 for ; Sat, 9 May 2020 11:44:40 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS2639 136.143.188.0/24 X-Spam-Virus: No X-Envelope-From: Received: from sender4-op-o11.zoho.com (sender4-op-o11.zoho.com [136.143.188.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 9 May 2020 11:44:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589049876; cv=none; d=zohomail.com; s=zohoarc; b=P09eEMAvEFcaEfJ7aHmNhSh9UQGAijYEqDMm3dovJdTmjhP4vgxdwfZcqEZEtqq7gSPWf+MWZR+BOwioh9coum208+yUsMdp+Lnf6JpT7vJrESobqEBnjWbwjL0rPHwFWue9aC1jrEYd7bYFPFmvW2cPfF660Bt8jLm/9DQQ3l4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589049876; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=405lxOGwm1ptzC01mcyICRC5+8j8A1otqRgOlYBSCSM=; b=Qq2rOC7aTrK8om7Qm1WjcD9e7TmPDLTG3QNBH0WB1CZs7BvJXQ2UrYipbw9IMxNgTLIngABIREBsdmGtx6sEVF6vGFVitqrMhYrmiqrnLx++5PeYgM40U5xCsbq1nKVXdFVeY6c8lfAi9yn6ZuS6yuffIkpwoI49Km7jCeiEhVA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=manuelcanga.dev; spf=pass smtp.mailfrom=php@manuelcanga.dev; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1589049876; s=zoho; d=manuelcanga.dev; i=php@manuelcanga.dev; h=Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=405lxOGwm1ptzC01mcyICRC5+8j8A1otqRgOlYBSCSM=; b=XbK4/PPjewKPaykgnHJV3rl5WD7Pmd1exPgOmyj5s72+Qn9V7deuKwbQsQu9d64d PxblFEivTe8pAeYDX3tIhKzgubl3/afSd5sVV52Pla59DwQMRHc/p3uwH8wjH0wNsP0 +W+41/ijvJGVlOpfrTQtOhZN3NjQBq9MhZU+Vtic= Received: from mail.zoho.com by mx.zohomail.com with SMTP id 158904984372883.13519394728382; Sat, 9 May 2020 11:44:03 -0700 (PDT) Date: Sat, 09 May 2020 20:44:03 +0200 To: "Bob Weinand" Cc: "Internals" Message-ID: <171fac045cf.124a18ecf610619.2220256095841059487@manuelcanga.dev> In-Reply-To: References: <171dee3baf3.bef5753260554.3262181919550021853@manuelcanga.dev> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Importance: Medium User-Agent: Zoho Mail X-Mailer: Zoho Mail Subject: Re: [PHP-DEV] [RFC] Keep type of reference params From: php@manuelcanga.dev (Manuel Canga) ---- En vie, 08 may 2020 23:40:22 +0200 Bob Weinand = escribi=C3=B3 ---- > > Am 04.05.2020 um 10:53 schrieb Manuel Canga : > >=20 > > Hi internals, > >=20 > >=20 > >=20 > > I would like to present a possible new RFC( "keep type of reference pa= rams" ) for your > >=20 > > consideration. > >=20 > >=20 > >=20 > > Firstly, an example: > >=20 > >=20 > >=20 > > ``` > >=20 > > >=20 > >=20 > >=20 > > function my_array_shift( array & $array ) { > >=20 > > $array =3D "string"; > >=20 > > } > >=20 > >=20 > >=20 > > $array =3D [ 0, 1, 2, 3, 4 ]; > >=20 > >=20 > >=20 > > my_array_shift($array); > >=20 > >=20 > >=20 > > count( $array ); > >=20 > > ``` > >=20 > >=20 > >=20 > > The result of this code is a warning( in count line ) because of $arra= y is a string.=20 > >=20 > > However, I think it should be an error or exception when a string is a= ssigned to $array var.=20 > >=20 > > In my opinion, $array var would have to keep its type when function en= ds. > >=20 > >=20 > >=20 > > What is your opinion ? Do you see it useful ? > >=20 > > Thanks and I'm sorry for my English( I'm a Spanish ). > >=20 > > Regards > >=20 > > -- > >=20 > > Manuel Canga >=20 > Hey Manuel, >=20 > the primary issue (apart from the BC break) here is leaking the referenc= e across the function boundary. >=20 > function a(array &$a) { > $GLOBALS["globalA"] =3D &$a; > } >=20 > funcition b() { > $GLOBALS["globalA"] =3D 10; > } >=20 > $a =3D 1; > a($a); > b(); > // $a is magically changed to 10 >=20 > Yes, you can here verify, that $a is an array at the function boundaries= , but you cannot afterwards. >=20 > If we had proper inout parameters (which do not leak a reference, but as= sign the value of the variable (in callee scope) back to the passed variabl= e from caller), then we could easily enforce it. >=20 > But as it stands now, this is not an option. (Especially due to the fals= e promise this seems to make.) >=20 > Bob Thanks Bob, that was a great example I explain...I am very forgetful, so it's very easy for me to write somethi= nk like this: ``` function filter_something( &string $to_filter) { $to_filter =3D strtolower($to_lower); $to_filter =3D sanitize_this($to_filter); ..... }=20 filter_something($my_string); ``` Sometimes, one of these functions can returns a false|null|numeric|... inst= ead of expected type.=20 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 =3D strtolower($to_lower); $to_filter =3D sanitize_this($to_filter); ..... }=20 filter_something(inout $my_string); ``` PHP could in the end produce exception when any function in `filter_somethi= ng` modify type of $my_string or =20 PHP could keep value of `$to_filter` as well if any function throwed an ex= ception. Regards -- Manuel Canga