Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109035 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 66976 invoked from network); 15 Mar 2020 20:06:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Mar 2020 20:06:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C42871801FD for ; Sun, 15 Mar 2020 11:28:34 -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,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 15 Mar 2020 11:28:34 -0700 (PDT) Received: by mail-qk1-f176.google.com with SMTP id j2so9373454qkl.7 for ; Sun, 15 Mar 2020 11:28:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:message-id:in-reply-to:subject:mime-version; bh=6ObWZOCfjOkXMiwlp/D4+fpGDETLlIFvCGYcB1wS34o=; b=zw4zJiFpCcSgdibHY2ijybYZy23V0YEmM4PEu2T/NP5h1UrAif6d+q1O7jwp5XPr3s zNgzoVt1TB6/RxZcBiH4+cQBa3wigYhvxm4CWd/c2QKZ4Dv8AoI5+SpL8AS++EnxNJlw Pgt84jJZC5uGEJHImrS0VGDCnpZviFbHEqH4WF65ruQbRuk6a5m9MfwTayfNRXuIyqZz JgAF5wDkr96310hmUTjR8CVFrII86bb1+p+YbJiHbSm4fXiL2QQHaM9VQHUS/mv4+9AB bPaAEGa43mYqLC29vdwe3xF3XeYd8wT957QIKodbeue5HPDrBJkxhwyfErua26MPVFgc e0gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to:subject :mime-version; bh=6ObWZOCfjOkXMiwlp/D4+fpGDETLlIFvCGYcB1wS34o=; b=uhXP1QWK3WKzzHkOyJWaIitnZlFq83GDgaDM00NOL2EfnBli+d2JxDSGi6TKPm+WSz lWpscVNuBdz3r+wCZ5XcwSporipV1hg5w51et8ZmrVYvbaPNSSbD0dRZ2zeLq3GVrGne y5zo9YjNpSNLQsT/Wck4NhUQ++c5LYt16D8WRVbp8vF5epAKXlfE63wFcJATyyJ/YrcK B/QyYU2KPS5s3jJwbmYbGF9Ev+0fqGYCz8ldjyQZmjnBNbLIeQJ0Yh8yAiDIzfk8zn3Q sA/16zBuPRw7WPwr4Av/VeqX+fv5IrOljkwMxuwL3BOOwJHwBBUkOjnz6Naqk42MgfAv +yYg== X-Gm-Message-State: ANhLgQ28E+lRyihRbPdqTVD4tN9kAOomkbb8ip9c98UtgRRLGJNfiw8h b/8+rp31I6iyzK/x5gebZL2Viw== X-Google-Smtp-Source: ADFU+vuwt7SZ0GPgGs1T2KLNIghj+E4ItwuLaSp2veXQv9Ng2i+EKFDBD540uuvZ4lmwYcPWy9unhQ== X-Received: by 2002:a05:620a:1136:: with SMTP id p22mr6517717qkk.131.1584296913231; Sun, 15 Mar 2020 11:28:33 -0700 (PDT) Received: from [2601:c0:c680:5cc0:b07a:f684:200:0] ([2601:c0:c680:5cc0:15b6:1f18:15b2:6db2]) by smtp.gmail.com with ESMTPSA id w16sm6645120qkw.37.2020.03.15.11.28.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 Mar 2020 11:28:32 -0700 (PDT) Date: Sun, 15 Mar 2020 14:28:32 -0400 To: tyson andre Cc: Internals Message-ID: In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="5e6e73d0_66334873_12dfd" Subject: Re: [PHP-DEV] RFC idea: Block scoped variables with "let $x =?utf-8?Q?=3D_?= expr" From: mike@newclarity.net (Mike Schinkel) References: --5e6e73d0_66334873_12dfd Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline =20 =20 Hi Tyson, =20 =20 Normally I would be one to advocate for new features, but in the case I a= m against the proposed feature. =20 =20 I know it is a feature from Javascript, but I think it is maybe the natur= e of Javascript prototypes makes it more useful though I am not sure as= I do not specialize in JS. OTOH the nature of PHP coding minimi= zes the need. =20 =20 More specifically regarding PHP, the purpose of your proposed feature a= ppears to be to allow longer and more complex functions and methods a= nd more deep nesting. However, I think we should not be encouraging peopl= e to write longer and more functions and methods and nest more deeply. An= d I have unfortunately have had to refactor too many long and complex= functions and methods and deep nesting written by others that I active= ly want try to discourage such approaches to coding, in PHP at least.= =20 =20 What I would rather see are new features in PHP that encourage developer = to break functionality into shorter and simpler functions and methods, = and to minimize nesting. =20 =20 The Software Engineering StackExchange seems to share my view: =20 =20 https://softwareengineering.stackexchange.com/a/200712/9114 =20 =20 There is also an issue with block scoping that is reasonably well-known a= mong the GoLang community called variable shadowning and that is a de= veloper seeming a variable in block scoping and thinking they are accessi= ng or assigning the function-scoped variable. =20 =20 =20 In GoLang it is more of a problem because of lack of explicit declaration= required for variable shadowing =E2=80=94 and the language designers hav= e lamented that they wish they never included block scoping =E2=80=94 but= even though the original PHP developer might not get confused as they wo= uld be writing the =60let's declaration another developer trying to maint= ain the code could easily introduce errors in the code if they don't noti= ce the block declaration. =20 =20 =20 =20 https://rpeshkov.net/blog/golang-variable-shadowing/ (https://rpeshkov.= net/blog/golang-variable-shadowing/) =20 =20 =46or these reasons I would definitely not want to see block-scoping of= variables in PHP. =20 =20 =20 =23jmtcw =20 =23fwiw =20 =20 =20 -Mike =20 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: =20 =20 =20 function =46oo()=7B =20 =20 > =24var =3D =22I am a function scoped var=21=5Cn=22; =20 > =20 > (function()=7B > =20 =20 > =20 > > =20 > > =24var =3D =22I am a block-scoped var=21=5Cn=22; > > =20 > > echo =24var; > > =20 > =20 =20 > =20 > =7D)(); > =20 > echo =24var; > =20 =20 =7D =20 =20 // Prints: =20 // I am a block-scoped var=21 =20 // I am a function scoped var=21 =20 =46oo(); =20 =20 =20 =20 > =20 > On Mar 15, 2020 at 12:47 PM, wrote: > =20 > =20 > =20 > Hi internals, In PHP, variables are currently scoped to the function s= cope, and can't be scoped to a block scope. This makes it difficult to re= ason about how a variable will actually be used at a glance, especially i= n long functions, or top-level statement lists of a file. (or how the var= iable was originally intended to be used) The function scope lifetime of = variables in PHP is similar to how JavaScript treated variables with =60v= ar=60, before the introduction of the =60let=60 statement in JS: https://= developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let = Would there be any interest in adding something similar to this in PHP=3F= (Or have similar ideas been proposed in the past=3F I didn't see any.) C= orrectness would be enforced as a best-effort at compile-time - the varia= bles would continue to only be freed at the end of the function call. (e.= g. =5F=5Fdestruct() only gets called when the variable gets reassigned or= the function call ends) - =46reeing it immediately would likely require = the equivalent of a try=7B=7D finally=7B=7D to handle exceptions. try sta= tements prevent opcache from optimizing a function, the last time I check= ed. =60=60=60 =7B let =24myObject =3D new MyClass(), =24flag =3D true; =24= myObject->process(=24flag); =7D // Accessing or setting =24myObject or =24= flag outside a different let is an E=5FCOMPILE=5FERROR // because it was = declared anywhere else in the function body // as a let statement =24myOb= ject->doSomething(); // E=5FCOMPILE=5FERROR to access =24key or =24value = after, outside of a separate =60let=60 scope foreach (=24arr as let =24ke= y =3D> let =24value) =7B // It would be a compile error to declare a got= oLabel: inside of a scope of a let statement. =7D // E=5FCOMPILE=5FERROR = to access =24key or =24value after echo =24=24var; // E=5FCOMPILE=5FERROR= to use variable variables in the same scope, to enforce this as much as = possible. =7B let =24outer =3D 1; =7B let =24outer =3D 2; // E=5FCOMPILE=5F= ERROR to declare in a different scope, can only reassign =24outer =3D 3; = // this is allowed =7D =7D =7B let =24myRef =3D&=24other; =7D =7B let =24= myRef =3D true; // This removes myRef from the reference group before ass= igning a new value to myRef - =24other is unmodified. =7D =60=60=60 I say= =22best-effort=22 because require() and =24GLOBALS and get=5Fdefined=5Fv= ariables() can still read or modify or make references to variables outsi= de of a =60let=60, depending on what's in the function body or top-level = statements. - Tyson -- PHP Internals - PHP Runtime Development Mailing Li= st To unsubscribe, visit: http://www.php.net/unsub.php =20 > > =20 =20 =20 --5e6e73d0_66334873_12dfd--