Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109046 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 17351 invoked from network); 16 Mar 2020 01:20:28 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Mar 2020 01:20:28 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B2FD91804D8 for ; Sun, 15 Mar 2020 16:42:44 -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 NAM04-SN1-obe.outbound.protection.outlook.com (mail-oln040092011083.outbound.protection.outlook.com [40.92.11.83]) (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 16:42:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lnt9FulAUhmKRaOjqHVZqr91Dg5Cqx/swU/ZV8yg+znewHM04bs6m2XKYlvVb/lli5a24g3m3wLdxHASEF6A5ChVYUULTDVHbw7zMm+CwM9aM2OiASP23gdAKq+khohRL+iukYDRAog+ynEUY79/zpK1a4FHhFu6zAO6p53pNaJv/0vcmf8KSLfHqvEqD9xFFcJiMkpOXjFXqH4CI8kacfLgF4FRqvYZF3Y+7BfTnZ89/azeXXP4qn9w7hJ8I8RbzKbX/KvBRERhMK4zOebmRM6W5dSklsdcBCE1w0a7bwg3UifZLh0NwtVcVcZLxpRefapokEKLOUJWQLFyiEE9nA== 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=n1N/G01op0So5uJIJ+Lu0vUSACKy74UTi5HCGy0HwSA=; b=XYJN84xfDp9B6FGu4YbIxGQSB7u4Td5A1KgjglQ+NqKBSXtzAdvXcX8uOi0X/o0CZ9ys/KoESmx2VW1VCs7lf7TQfECK5v6UXN+rR6vpuLnVTUKkcU+i7opy2ef44eNgMD/IvgDazK6bSTw6vO4hqRlF/Jyjn6+upxr4q7vcD1pc6xX/exO9nEg8zatLIN32wzZNXu3VxeHYGjc1N/oW+5e2kR7QP17UAFUgQr3wR9E+wCBsnxJn6Tq/qowy3V6Gpo1AVZVbAy9V458rJjt5UXlV/Qk9gl9EUZqaouaAPQ15L/3dDT7ewrTnwGwk2GhYOV58tZjkEpYnrnPCbQCuLg== 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=n1N/G01op0So5uJIJ+Lu0vUSACKy74UTi5HCGy0HwSA=; b=Y9trufHxZ65zkSTOJ1t6Q7C8DplQY2IPeTxJttVfumyLI/GQ3edDdb9NGFNoGwgPZjMt4xZtLJ4HYMz7k2eQTc5FukSIu/ABXNpFFeCy60HBeTCaMIO51mdYLUAMHLnMQOgaBHuYJN6voVfE8r/OMshLbTR1M1+GxxqZyryGwpY1FxMyifjDJxwpKCfYlgl/j+CBosmLH1WshRqwuY8O+pNmybm4gmr+Yy8WU5/scFjdFMna7Sw3WphZrZiEmJWbY44g5HYKZ2AB2Fofu366YaQB8YYk9iqExnuIcE/u1jbDzdgnmx0M/twI/iZoCt0qCXSCxH0vB9FH/k4FReD62g== Received: from BN3NAM04FT020.eop-NAM04.prod.protection.outlook.com (10.152.92.51) by BN3NAM04HT034.eop-NAM04.prod.protection.outlook.com (10.152.93.65) 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 23:42:41 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com (2a01:111:e400:7e4e::34) by BN3NAM04FT020.mail.protection.outlook.com (2a01:111:e400:7e4e::167) 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 23:42:41 +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 23:42:41 +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+xQCgAA0sACAABcx3g== Date: Sun, 15 Mar 2020 23:42:41 +0000 Message-ID: References: ,<30C23964-6E52-4B2B-8947-8BD3E9E46354@newclarity.net> In-Reply-To: <30C23964-6E52-4B2B-8947-8BD3E9E46354@newclarity.net> Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:28EC6010C2FD135DF1913656A07F2124DEA0EB06E9EDC20188FFAC0CCDC43D33;UpperCasedChecksum:E6A01F961807352FA1CB6E29191C02F895FA7C476EC0C95B7AFC7552DF990C52;SizeAsReceived:7353;Count:46 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [cUpu0dymTOM1HMDa0EZdU2HW2JVhRjtRTtZ9nyKpQVBegyNqDE+AULbPxbpVUHhv] x-ms-publictraffictype: Email x-incomingheadercount: 46 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 10503d5c-673d-40a9-abc8-08d7c93a8d65 x-ms-traffictypediagnostic: BN3NAM04HT034: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Jj1uUWyl5BKIFedvBJiy0YisOGbmmt0brj4tLoQCBscnfHh8THVsBSlU0QoY+2XxnIFo5HkKPpFUCh4uPZjOGxJvx4oR0DhWht+mULG3MO2dp2weOBe1XerEhbyc6rEnr4yUsEk+CI8T8Oy9ID2slJinD+4PPhD2AmfBSf4l6PfSKB4DjSOtCroSyWkPa7Z0KswVNq3y/laCAj/Zig9PWsJeyLp2+M7GkaaHje0v30Y= x-ms-exchange-antispam-messagedata: 9aOlW6S8Pb78LYCfenap1kf2wSrivrwGY1KlogI0jwwp0A5sMvWlUD9fjrBBYs8tgEXS4DMFQ1+xRPWbdUAcyR8p+HAoQgBi8Oa8baGRg+l8gD2tXecyJCst3RUF67oOLhnyuIjXpZpk1ECA9D0GjLfpdVhVCZg4Z/L6eQneS1gaW3ZflGCqAZebRjTB9Qy3np+mImbDtrt4TEIBqntH1Q== 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: 10503d5c-673d-40a9-abc8-08d7c93a8d65 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2020 23:42:41.3231 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3NAM04HT034 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 am about to say is ironic since I too often feel people are trying= to enforce their view of coding on the world,=0A= > but I do not think we want to give people tools to make it easier to work= with long functions/scripts.=0A= =0A= Blocked-scoped variables is a tool which can make short code and long code = more readable.=0A= Making it easier to work with long functions is an example of a benefit of = having block-scoped variables,=0A= not the main goal of this RFC idea.=0A= =0A= https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/scope%20%26%20closur= es/ch3.md#blocks-as-scopes=0A= goes into some of the other reasons to use block-scoped variables in JS=0A= =0A= - Many languages other than JavaScript/PHP support Block Scope,=0A= and so developers from those languages are accustomed to the mindset, whe= reas those who've primarily only worked in JavaScript may find the concept = slightly foreign.=0A= - Block scope is a tool to extend the earlier "Principle of Least Privilege= Exposure"=0A= from hiding information in functions to hiding information in blocks of o= ur code.=0A= - Developers may prefer to check themselves against accidentally (re)using = variables outside of their intended purpose,=0A= such as being issued an error about an unknown variable if you try to use= it in the wrong place.=0A= Block-scoping (if it were possible) for the `i` variable would make `i` a= vailable only for the for-loop,=0A= causing an error if `i` is accessed elsewhere in the function.=0A= This helps ensure variables are not re-used in confusing or hard-to-maint= ain ways.=0A= =0A= > I would be very interested in seeing business logic coded into a long fun= ction that literally cannot be made better by refactoring into multiple sma= ller functions.=0A= > If you have some, please do post to a Gist and share it to see if it in f= act it cannot be refactored and made "better."=0A= =0A= One example is if PHP itself is used for HTML rendering in top-level statem= ents - pages or components of a page may be spread out across hundreds of l= ines.=0A= This could be moved to a different templating engine for PHP instead of raw= PHP,=0A= but the refactoring would be time-consuming and error-prone.=0A= =0A= Separately from HTML rendering, some problem domains are just complex.=0A= For example, consider the functions such as decodeMPEGaudioHeader in=0A= https://github.com/WordPress/WordPress/blob/master/wp-includes/ID3/module.a= udio.mp3.php=0A= (I'm not involved in that project or file)=0A= =0A= You could split it up into multiple helper functions, but instead of hundre= ds of lines in one function for a complicated file format,=0A= you'd have to jump across even more abstractions/lines to know where XYZ wa= s set.=0A= Fully refactoring that might not make ever make business sense,=0A= but there is a benefit to having the ability to mark if a variable is actua= lly block-scope or used elsewhere=0A= (e.g. to have a better idea of if refactoring to a different function is sa= fe)=0A= =0A= > Given the nature of long functions, I think the awkward syntax here provi= des a nice disincentive to writing overly long functions.=0A= >=0A= > But as I said, the closure does exist in case someone absolutely needs to= control=0A= > the scope of a block within an existing long function, such as the 1400 l= ine function I've been refactoring for months now...=0A= =0A= Also, closures aren't a convenient substitute for everything the local scop= e can do for other reasons.=0A= E.g. modifying by reference will mask issues with undefined or possibly und= efined variables when modifying values outside of the closure scope.=0A= =0A= ```=0A= function compute_product($values) {=0A= // Other lines omitted=0A= foreach ($values as $value) {=0A= (function() use (&$product, $value) {=0A= $letVar =3D $value + 1;=0A= // Other lines omitted=0A= $product *=3D $letVar;=0A= })();=0A= }=0A= return $product;=0A= }=0A= ```=0A= =0A= This has the bug that $product was never defined, but no notices are emitte= d because $product gets set to null=0A= when the reference is taken by reference, and php currently doesn't warn ab= out multiplying by null.=0A= =0A= Using an actual `let $var =3D $value + 1`, this would get the notice for th= e variable being undefined.=0A= =0A= - Tyson=