Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109038 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73563 invoked from network); 15 Mar 2020 20:31:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Mar 2020 20:31:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 643221804DD for ; Sun, 15 Mar 2020 11:54:01 -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-CO1-obe.outbound.protection.outlook.com (mail-co1nam11olkn2087.outbound.protection.outlook.com [40.92.18.87]) (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 11:54:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aAMOmg72XzP18njws97vkwCP6DIOfYy1gYQpIIw924SaZTk9DIa8yLM5kk4TwzSw2McLYfW0x/s6jkaKCn4k2syeJt/hTAaDAOOJplZySzCfyjsoGrvIafTAaMGpUz9ZDZicMmm1ykIEl3C+ssn9NhpO4fttBRxx6Xq0DgJqz0QZAhdDpH4UHcH50dkhwbt/XPQaZix/uxj7Z2L4TG+kfEm93zbTpwTXgkdMBaHgtg2x1oBC2VQO3+d/jxEO/jUq3JtygCD59YXJP3SMYtY5fnsZxMXVyABdFPj1VlofXuN4RgROj76QqN5hJDu4rDzK/YU6t16IX95pz2CkTiz3Sw== 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=QxeCh5cioFRuPVgVuLToe39Moe6OnkKq0shgREjS78k=; b=DYDwqu4deSINs8WXrfsfQ9q3ex3rVGX+0pserfovmj+p6OZRpJ8SLucjNbddA8tXt+yyzaVyI/UKXPV0LEaKC/33IK03oSLXTsb/1eVknU0KkxF5aU3lQIyxg/zhgd3PJSiWhi3S/Q/mXymX87RjjFfO8toOQV4UcfrnBKnuRN7kmqmr80eKoE8XiSjkyTN2FMN3KeLJuxfGEH7Kn4fGzt+5VRase/QRXhZofyb/XyW4t5M5+mvauzoFOo/3pfTA+Xus7iFPM3uPRJCYaDVRysZWisPsL66NZxu6tzwsZv0D8I2G7LROL0IZE6DfCxbOh3xSh5J3ts3mYBBt95pMgQ== 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=QxeCh5cioFRuPVgVuLToe39Moe6OnkKq0shgREjS78k=; b=uMenH5cm9UbE+poYx2emxnaLfK++kdxoCAQOdCVzSclSmpZK677E8EsjTU2eJ3+Qaxstirh4RMj5wt8iuL7hA3HTIC7Pj2TFBOFGj3DayvUpiLoTZlIoMvkfY3SWi0v9HMNXuPuJzZzhM46C79xZGrDhiA7CzP3PA95dc20fB0LOtZJ0Ci7LLPlrSqRh0cesZY2i81Wsp7EDvRfIQE9lGw4psC7HHGtd0OOV0OIKVBfHVqoU9lpfXiN9/s9A3+6v0NZVGlyB5EFqI2Ob1pmtE6T9+KNVDLv2qVbRF3gS5yl6PASVlBhFsSHWyT//7Cv08bZZ/hNG94JRfsWi0K1hng== Received: from CO1NAM11FT059.eop-nam11.prod.protection.outlook.com (2a01:111:e400:3861::36) by CO1NAM11HT077.eop-nam11.prod.protection.outlook.com (2a01:111:e400:3861::473) 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 18:53:58 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com (10.13.174.59) by CO1NAM11FT059.mail.protection.outlook.com (10.13.174.160) 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 18:53:58 +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 18:53:58 +0000 To: Mike Schinkel CC: Internals Thread-Topic: [PHP-DEV] RFC idea: Block scoped variables with "let $x = expr" Thread-Index: AQHV+veJX4ttr7o+RE+uPBxQ97wSIKhJ+xQC Date: Sun, 15 Mar 2020 18:53:58 +0000 Message-ID: References: , In-Reply-To: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:A89486BA7B65A4A29DF770131A5507E03E809FFEC37A7FDBE030CAA710675A3C;UpperCasedChecksum:73B26AEB955642F4E03DD4E699124EDC9AE07D579FA9698677B419CDE34DDF2B;SizeAsReceived:7207;Count:46 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [xiazgsWHcm2xuokX8QOBvtGPp59a2w2I9/SKR4cYseuzk1B+GWkmjMxP3GRE7jRN] x-ms-publictraffictype: Email x-incomingheadercount: 46 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 00d23637-c2d9-4888-59b3-08d7c912385d x-ms-traffictypediagnostic: CO1NAM11HT077: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: rYFAryOasOq7+02U+1WwdOU1HHiml8NjsFlAjSvfp1/10ji2/DPEnB6GGgvsZAfcNpmHgFZzwTLu600tAebLakAu6zBev1JVfvOr6B8ePn8CzCO6U8r22F0uFk8Lale5+Nqgj0QCYXrCShJshZihdp/tZSyn+mwOb+95AGRLv4AiHBntVSW0VavvECcbJv2R x-ms-exchange-antispam-messagedata: OPJjLP2mYikwzf9tSLpEbLWDAFkDT0wTcs+XLeAhFLBcBhB9xOjVDSAbIkuXnrcLsHiHPG5Tsf/b9kS+x4RIBktjqgFRXQJbEakz0n0di/y8BH69TncPcRUDHgvCSENsImSiUz9vZMHrsXOZm4dmRPPtJDidUBNDyccvykWMUXq9Y8vrnLfdDzN9xhikbrXkpR1GcOlgCsgHtFfs4iBEpA== 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: 00d23637-c2d9-4888-59b3-08d7c912385d X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2020 18:53:58.7121 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1NAM11HT077 Subject: Re: [PHP-DEV] RFC idea: Block scoped variables with "let $x = expr" From: tysonandre775@hotmail.com (tyson andre) Hi Mike,=0A= =0A= > What I would rather see are new features in PHP that encourage developer= =0A= > to break functionality into shorter and simpler functions and methods, an= d to minimize nesting.=0A= =0A= I would also like to see shorter functions in general, but:=0A= =0A= - Other developers may prefer long functions/scripts despite my preferences= ,=0A= =A0 but their code would still benefit from readability by having block-sco= ped variables enforced by the compiler=0A= - Some problem domains are complex (e.g. business logic) and require a lot = of variables,=0A= =A0 leading to long functions/scripts, and trying to split them up may be e= rror-prone=0A= =A0 or cause awkward divisions of functionality.=0A= - Even in short functions of a few dozen lines, it may be useful to know if= reused variables such as=0A= =A0 `$i` are definitely scoped to a loop, when reviewing code.=0A= =0A= > There is also an issue with block scoping that is reasonably well-known= =0A= > among the GoLang community called variable shadowing=0A= > and that is a developer seeing a variable in block scoping=0A= > and thinking they are accessing or assigning the function-scoped variable= .=0A= =0A= Variable shadowing is also an issue in JavaScript.=0A= While JavaScript and GoLang don't allow introspection of the variable scope= =0A= (as far as I know, not sure about reflection),=0A= PHP does allow dynamic access by name ($$var, `get_defined_variables()['var= ']` in a require()d file, XDebug),=0A= and any proposal for block scoping in PHP will need to account for that som= ehow.=0A= =0A= - Aside: Personally, I set up linting for golang projects to forbid shadowi= ng.=0A= =0A= My best plan for dealing with variable shadowing in PHP was to forbid it en= tirely,=0A= which I mentioned examples of in the proposal.=0A= =0A= > {=0A= > =A0 =A0let $outer =3D 1;=0A= > =A0 =A0{=0A= > =A0 =A0 =A0 =A0let $outer =3D 2; =A0// E_COMPILE_ERROR to declare in a di= fferent (shadowed) scope, can only reassign=0A= > =A0 =A0 =A0 =A0$outer =3D 3; =A0 // this is allowed=0A= > =A0 =A0 }=0A= > }=0A= =0A= > P.S. If you really need block scope within a long function or method you = can already create a closure and call it in the same expression:=0A= =0A= I'm aware of this and have used it (e.g. to avoid modifying the global scop= e in large projects),=0A= but avoid it in functions due to the awkward syntax and the performance ove= rhead of function calls.=0A= I'd generally just use a different variable name.=0A= =0A= - Tyson=