Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109029 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 49146 invoked from network); 15 Mar 2020 18:25:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Mar 2020 18:25:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A7C161804DF for ; Sun, 15 Mar 2020 09:47:32 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8075 40.64.0.0/10 X-Spam-Virus: No X-Envelope-From: Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11olkn2084.outbound.protection.outlook.com [40.92.20.84]) (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 ; Sun, 15 Mar 2020 09:47:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S1DQvc0lbC/hGa/Y/lNAMZ1pPeSTrsdJZmnS4398B6Wcuo9+pm3DsvD3KTmu/wbuw9WRgMNeKsJhX6HlrYAPS8w2gFuKr6tLqTjA5YuRoWAE+k7qbUocxkGs/BaeuH2fwO4+x9JFDoTaCSmHQHjME8slnwEkE6rYQLqCqSkQRWSfhF5glicmBA+CeOLlFBRNPzcT2emCJeDLnFXv8xDjfU6+lD6feHlV9N0KCXQzmyrQAkZS1XGmgwQiYYafmH7EerzhOTytyPxGoB5XHktmG9BgURdrSRePPMVVqaff6COoIoekh/weuOwpq2kwZ8LM/MJMxjOWQoLZFcKSM8m18w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DiEvsIoE3s0ESIMMg0G4JyBY66TqJS0whPtjoUodHik=; b=Q4MiFQM40zlyxw78k7e+n3CkjlP6zeR34Rg9JB1jrNEKhCMagvcU7zUfftU3RvsZMiSi4oUQ2EEARUSVaBE+Z2bwYc44HYHe2kGlHswpRcMf+J5hzw0WWne+bTH4Ycfj+l+5FB7gXKEJoKo8qpO64IghPRhMbP6LVyJqXbLQsUVGEnaXIQKmR6hqychiaFDwLQ0Na1EwNJQFVEa08QB50zQOLr18mm2OyeXF3nZCke1YuUspotPS0s8Pg4dE/7P0PfA27IZh36239yKxJabVKR5OpCuyw7m9n/Jk/nCiJ2hRjCyNWDpqSC//O3aOl8pgMLwBGqJ+2usDihguTMIygA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DiEvsIoE3s0ESIMMg0G4JyBY66TqJS0whPtjoUodHik=; b=DxIb9u/kPqLqQdCxPPVV0eMjb1MBu6Sjm7wzTD8IB0VHwyqtSTjsfQu7hgF2g5SMRMULcPzRGceyWVT/t/PPNupoTtsyOYrjSBJ9reADQXTajUY5KfASnQ5XM2OCJig79OwVF43oDu5gtEurv1ls11RP2/FPd/IzJ1eLC6GuhKb/c/lJG78i9TBVjkt+Acpqwu1eCOvGsqfBwjpYP/sBYIPue4tSIa6anIaeUiwfk+0svax7xlSpDWzAWentLrb5SSqPlN+MPZ4ej6nGy7o5NHrooZjwnduCUykFzYHwT+li27f0AN1Dln4YraC9CijKr94FMPtAO+i0RSTABsfTlA== Received: from DM6NAM11FT025.eop-nam11.prod.protection.outlook.com (2a01:111:e400:fc4d::37) by DM6NAM11HT191.eop-nam11.prod.protection.outlook.com (2a01:111:e400:fc4d::310) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2814.13; Sun, 15 Mar 2020 16:47:30 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com (10.13.172.59) by DM6NAM11FT025.mail.protection.outlook.com (10.13.172.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2814.13 via Frontend Transport; Sun, 15 Mar 2020 16:47:30 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com ([fe80::1133:bcac:caf1:d588]) by DM5PR07MB3067.namprd07.prod.outlook.com ([fe80::1133:bcac:caf1:d588%3]) with mapi id 15.20.2814.021; Sun, 15 Mar 2020 16:47:30 +0000 To: "internals@lists.php.net" Thread-Topic: RFC idea: Block scoped variables with "let $x = expr" Thread-Index: AQHV+ug0CGaQ9aFqj0aNq48f2k+kqQ== Date: Sun, 15 Mar 2020 16:47:30 +0000 Message-ID: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:351110EB151BD93C9AE717E7B4BC165B42EB5CEE12230FE0CF118A8E78EA9FD5;UpperCasedChecksum:DF691F570D0E781E53822F7D2E50C9BF4C53584DC6F72E3F729DDA86FB3CA477;SizeAsReceived:6886;Count:42 x-tmn: [ApHT1F3tbjxrwUfWJJ2dDE8jN2i3/wo0lIaOsDx3bqb2v91Rfqepr/5xjCEqhagm] x-ms-publictraffictype: Email x-incomingheadercount: 42 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 69b74b9a-9699-4798-a9da-08d7c9008d9e x-ms-traffictypediagnostic: DM6NAM11HT191: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: rSQ2rQNoVrx7CpN1cqjbR7/XeluoJsc5ca6CQcrSya0BDuElBPGafsZuUshmvM60AsxIyWTmJT93grubS2grOgERF2pvWueXtRXSuP9isO2F34Uy6kGC+6dZ2xd6We3GDnJf43Vo7R+TnWYM2iAF0rN8oDLtAzbAg13/Q0yEGDaT5LdIlJTlH8kcjHRARBC4MYeIKwecNnN9bBmIHXXH2yp9qoqyxLcMST1tQdrIEIc= x-ms-exchange-antispam-messagedata: NF+BLIH+TLQP9wZrMcN7Vw53fu+0KzQULSy8kVAfUg1bsCqRMRYBiibEtjNQLM86tbdYXOJs9aErFYl0DFYX/pHrvNB7pcn84Cyou/RTfD+bny7jkQuDx5rI3ujNd3Ps6RkInIuA1HZs0FEESwmaIG8rSJZ+4+nHpAUukkjc1NoiuzWb9ZJ6j3E7F/WSQOL+VXeINNcA4MbtSLjOXS63tw== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 69b74b9a-9699-4798-a9da-08d7c9008d9e X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2020 16:47:30.8213 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6NAM11HT191 Subject: RFC idea: Block scoped variables with "let $x = expr" From: tysonandre775@hotmail.com (tyson andre) Hi internals,=0A= =0A= In PHP, variables are currently scoped to the function scope, and can't be = scoped to a block scope. This makes it difficult to reason about how a vari= able will actually be used at a glance, especially in long functions, or to= p-level statement lists of a file.=0A= (or how the variable was originally intended to be used)=0A= =0A= The function scope lifetime of variables in PHP is similar to how JavaScrip= t treated variables with `var`, before the introduction of the `let` statem= ent in JS:=0A= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statement= s/let=0A= =0A= Would there be any interest in adding something similar to this in PHP?=0A= (Or have similar ideas been proposed in the past? I didn't see any.)=0A= Correctness would be enforced as a best-effort at compile-time - the variab= les would continue to only be freed at the end of the function call.=0A= (e.g. __destruct() only gets called when the variable gets reassigned or th= e function call ends)=0A= =0A= - Freeing it immediately would likely require the equivalent of a try{} fin= ally{} to handle exceptions.=0A= try statements prevent opcache from optimizing a function, the last time= I checked.=0A= =0A= ```=0A= {=0A= let $myObject =3D new MyClass(), $flag =3D true;=0A= $myObject->process($flag);=0A= }=0A= // Accessing or setting $myObject or $flag outside a different let is an E_= COMPILE_ERROR=0A= // because it was declared anywhere else in the function body=0A= // as a let statement=0A= $myObject->doSomething();=0A= =0A= // E_COMPILE_ERROR to access $key or $value after, outside of a separate `l= et` scope=0A= foreach ($arr as let $key =3D> let $value) {=0A= // It would be a compile error to declare a gotoLabel: inside of a scop= e of a let statement.=0A= }=0A= // E_COMPILE_ERROR to access $key or $value after=0A= =0A= echo $$var; // E_COMPILE_ERROR to use variable variables in the same scope= , to enforce this as much as possible.=0A= =0A= {=0A= let $outer =3D 1;=0A= {=0A= let $outer =3D 2; // E_COMPILE_ERROR to declare in a different scop= e, can only reassign=0A= $outer =3D 3; // this is allowed=0A= }=0A= }=0A= =0A= {=0A= let $myRef =3D&$other;=0A= }=0A= =0A= {=0A= let $myRef =3D true; // This removes myRef from the reference group be= fore assigning a new value to myRef - $other is unmodified.=0A= }=0A= ```=0A= =0A= I say "best-effort" because require() and $GLOBALS and get_defined_variable= s() can still read or modify or make references to variables outside of a `= let`,=0A= depending on what's in the function body or top-level statements.=0A= =0A= - Tyson=