Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109034 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64638 invoked from network); 15 Mar 2020 19:55:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Mar 2020 19:55:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CFD911804DF for ; Sun, 15 Mar 2020 11:17:43 -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 NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10olkn2106.outbound.protection.outlook.com [40.92.40.106]) (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:17:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ccoXek5vSPKm6mhsOZAJEecAtiRWgvqb6+YyyFDXk7yrfdYrl/cwKfVX0pkeGiiHjgsSpwMN9xnuA9wzRt19R5wB2FoJ8Mw5cI35TsIIPn+MEn0tD+mMwIpef9aB/s+/6FLK9Wk3NCWmzZgMxVz8HhXwuTsclnicf+7Xyd0Q660kQQbhDhkjnC6fiCLonGRu31xxqLth6RQz/+y4s1FlxlXs9RZsR2cA3wfG98+t8bryV68pyl8gkz4pUHMYRbBvN0UtBeukIocHbzsXNYL25qleAjXfUcETl67g205TxjdaBiqZ6G/CTYglXpKsS0FVim2wYRM3wRySl3oFtC2p2Q== 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=3yATV4f6FhoW/gSRQEc3KDbQoqBk/gUxxpUD4mxiU/o=; b=hEsQwUKO/pVaZVdpMEuC/Y9MvMiMmS+LtUFmMRTFlwaaHHRwyjjQnPgo3Na8TF63KMnZCQES56VZSKKP9ET0s3RXY+e28IHj+sFc8CQKhbkPIP1bMrJV4U0MrlVpqFI4X8ANFjpD0voqp/pzDpKv1oNl7b/jHznJDkmicd7WmTlR4fsLaEN6v0INKrW081ofrJh3Erccj2YzdjXWzVx0/GlEg+Z9ZJgBPVOn52PSfiC/xUWdgMJ3gAix46tdJ/XxIU03rIy2xEAreLW+83owPfO919K79kVVMmG0noCZI+maqp2eiLSaaC/fV9xC8c68zobDm1K239oh+IU6mvlCqg== 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=3yATV4f6FhoW/gSRQEc3KDbQoqBk/gUxxpUD4mxiU/o=; b=ReScgLkwuzdHVMSnQBrFLAwEHHavtpI1AeyN6Y8rqKHUCOzArZ3ehgPFsg2EN9lOrmotahaPxhombFQrCZ6SP88Xnkvpnm0I0fpFahtbbwktL+DG3Q1RTHVw+OyIJn4JiXvzTS6+PJ8Z/da7/jaYRe4okqr6HnDUSvKGmFaQufym7MIw+Udd03nPzb2wV8J8zKccEXqIs5fwL123J5s3d4oniiU7Opr2FVMeK8KRjsfHeTdEwFYLGbhNihRU/vPpTMCSI4TfxGT0gfT4dRKiqW/kcoyLPuIvP76KEOjgb70XAOSclXhkIVosHEFHLlzPlNbUB9CehK11VD+Dcv3XUQ== Received: from BN7NAM10FT042.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e8f::35) by BN7NAM10HT094.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e8f::380) 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:17:42 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com (10.13.156.52) by BN7NAM10FT042.mail.protection.outlook.com (10.13.156.218) 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:17:42 +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:17:42 +0000 To: Rowan Tommins , "internals@lists.php.net" Thread-Topic: [PHP-DEV] RFC idea: Block scoped variables with "let $x = expr" Thread-Index: AQHV+ug0CGaQ9aFqj0aNq48f2k+kqahJ7oQAgAACegI= Date: Sun, 15 Mar 2020 18:17:42 +0000 Message-ID: References: ,<77731b0f-70d9-788c-f6bf-7663542aaa7b@gmail.com> In-Reply-To: <77731b0f-70d9-788c-f6bf-7663542aaa7b@gmail.com> Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:750699BE87256AECEA755DFCFD8EB48734AEAE3970C633931F67152F7144805B;UpperCasedChecksum:E7A499BFB253CCD49D6ABBF521BC60F42CCE819831155232CB7C6160A712E5C7;SizeAsReceived:7221;Count:45 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [Q/pr5f5kmcmAZxBM8sPFHCFMTh5Xl2m30Yp171lLc3R8L3xJ/jmfnTVqkYIk7Yfi] x-ms-publictraffictype: Email x-incomingheadercount: 45 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 9edf67fb-d5ff-479f-9641-08d7c90d2725 x-ms-traffictypediagnostic: BN7NAM10HT094: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Y7nb7RzPr9mh/JsWuW62ASMb2hxLcs2YhoNbkvyZHK7L0lFOsu1yr4LIwYfYxodLNiMxkcBeepIhOKW0ufyTdQa6D6ePjESVKMMi2fU0X94Pm00ZwwdvEBUdjt/W8I2Z639iBVvFXkYdrizZW0n2Y2Oer+XDxq5pV0U4bwx/apRSFkj1fFqJS+6V6XkVd/k0oSRDQFPgNRMwR5/WLtaOTg3QfZgwutFba/hG1qOfXl8= x-ms-exchange-antispam-messagedata: 4Oy1uV1QAsXs7Z+L3K6LtCrY3929tf+0O7XykhFLnG+pKcwLCBVcIlkokkpdR4Epu6a9Vl5Tex/G/ho82fd9ElYq17Q9oYQgC/ZXUknI8TI5R92C1PIdynAB2hG0sMwJ723nNIoH/I8NN/tgPVUGXLdut3YuyLM18qSpgGT8H9dU0kAkXS/eo8mqKFejsGQ1H8WsYTgSz7bAe9vj/QFsRQ== 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: 9edf67fb-d5ff-479f-9641-08d7c90d2725 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2020 18:17:42.3507 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7NAM10HT094 Subject: Re: [PHP-DEV] RFC idea: Block scoped variables with "let $x = expr" From: tysonandre775@hotmail.com (tyson andre) > Intuitively, I would expect this:=0A= >=0A= > {=0A= > =A0 =A0 =A0let $x =3D new Foo;=0A= > =A0 =A0 =A0$x->bar();=0A= > }=0A= > somethingElse();=0A= >=0A= > to be equivalent to this:=0A= >=0A= > let $x =3D new Foo;=0A= > $x->bar();=0A= > unset($x);=0A= > somethingElse();=0A= =0A= If this feature freed variables when going out of scope,=0A= it would be compiled more like the following (if there were multiple lets):= =0A= =0A= JavaScript doesn't have destructors, but PHP does, which makes the implemen= tation a tiny bit more complex.=0A= =0A= ```=0A= try {=0A= =A0 =A0 $x =3D new Foo();=0A= =A0 =A0 $otherLet =3D new Other();=0A= =A0 =A0 $x->bar();=0A= } finally {=0A= =A0 =A0 // try/finally is useful if unset($otherLet) could throw from __des= truct=0A= =A0 =A0 try {=0A= =A0 =A0 =A0 =A0 unset($otherLet);=0A= =A0 =A0 } finally {=0A= =A0 =A0 =A0 =A0 unset($x);=0A= =A0 =A0 }=0A= }=0A= somethingElse();=0A= ```=0A= =0A= Actually, thinking about this again, performance is hopefully less of a con= cern.=0A= I could safely avoid using `try/finally` if this was outside of a `try` blo= ck,=0A= as long as this was in a function scope instead of a global scope.=0A= (All variable go out of scope for uncaught exceptions)=0A= This idea is still a work in progress.=0A= =0A= It would definitely be useful to free immediately for objects (or arrays wi= th objects) which call `__destruct`=0A= when the reference count goes to 0, such as RAII patterns.=0A= =0A= My concern about adding an extra `unset()` is that it would add some perfor= mance overhead,=0A= discouraging using this feature, but opcache should be able to optimize the= unset out when unnecessary.=0A= =0A= I touched on the reasons for avoiding `try` blocks earlier, but hopefully t= hey're not needed.=0A= The global scope is less likely to be vital to performance in most cases.= =0A= =0A= > - Freeing it immediately would likely require the equivalent of a try{} f= inally{} to handle exceptions.=0A= > =A0 try statements prevent opcache from optimizing a function, the last t= ime I checked.=0A= =0A= See https://github.com/php/php-src/blob/43443857b74503246ee4ca25859b302ed0e= bc078/ext/opcache/Optimizer/dfa_pass.c#L42-L49=