Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111413 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 12456 invoked from network); 10 Aug 2020 07:47:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Aug 2020 07:47:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 918E118050A for ; Sun, 9 Aug 2020 23:46:55 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from sender4-op-o11.zoho.com (sender4-op-o11.zoho.com [136.143.188.11]) (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, 9 Aug 2020 23:46:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597042012; cv=none; d=zohomail.com; s=zohoarc; b=M/G1nkGIRaTSoDoDRfmaoE060YaUVi9YcqYDaxD096eQgRoY5vbZ6wXT4s8FdVTRNcvFfGm8p5SK+ttPpX/9Ggyn6TRNPkZ9QHOsG6jW64BLwdwwq0MLwEjOkg0oD7fZIhIsqkQ2uVT9YiNKVp8vWegrfJ31pJpC3QkJZgl1AKA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597042012; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=A0D7BDxA/zIHNDmv4ALAkC3XU5XUBveNbqkmUXWaGoI=; b=EPDiIVpt/YGmjkWXVvfnuvbmN6qx7ZwCIJ+j4qC2mF8OpT3Kv6d/J+fVnr0AhAipyY8/Th1UUSMGt7rMVEPfZFPTrxrXbKoaAMk5fa5C6MR49klhJbu0AjYGv2w0frCYhSO7DGMGj2MpOZBBJu7ur4ZspGWh0mQ2M+zIAaGdvrs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=manuelcanga.dev; spf=pass smtp.mailfrom=php@manuelcanga.dev; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1597042012; s=zoho; d=manuelcanga.dev; i=php@manuelcanga.dev; h=Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=A0D7BDxA/zIHNDmv4ALAkC3XU5XUBveNbqkmUXWaGoI=; b=Fx+FCPJ4kFwGDUNmHpKOlb7sAhFa4OTicSO8y0ZrVSA2dADIIrjNLZgoFhfF/12Z 6I8Uv4paPmvwXwrCc0HB3ewE3gY10ONUTFTRpU5GTQjIVrHL5FH3wXpTE9kz118ayCw OgMdWSG+Y5WMM5XZVye5MdHbK8ArfV4ZTZH4sl7E= Received: from mail.zoho.com by mx.zohomail.com with SMTP id 1597041979770534.3292053873246; Sun, 9 Aug 2020 23:46:19 -0700 (PDT) Date: Mon, 10 Aug 2020 08:46:19 +0200 To: "Marco Pivetta" Cc: "Internals" Message-ID: <173d71eb175.11ec80fe0613982.1532514557831836899@manuelcanga.dev> In-Reply-To: References: <173d2736435.eeebc066559719.6668730031195302987@manuelcanga.dev> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Importance: Medium User-Agent: Zoho Mail X-Mailer: Zoho Mail Subject: Re: [PHP-DEV] [RFC] Import of variables From: php@manuelcanga.dev (Manuel Canga) Hi, Internals, Thanks, Marco and also to Rowans, Josh, David and Tyson I'll use IIFE in order to avoid global conflicts. Regards ---- En lun, 10 ago 2020 02:16:06 +0200 Marco Pivetta = escribi=C3=B3 ---- > Hey Manuel, >=20 >=20 >=20 >=20 > On Sun, Aug 9, 2020, 11:02 Manuel Canga wrote: >=20 > > Hi Internals, > > > > I'd like to open up a discussion around the implementation of a new > > functionality: 'import of variables'. > > > > This functionality would allow to create a new 'use vars' keyword in > > order to can use( or cannot use ) global variables in local scope( of > > current file ). > > > > I think the best is a example: > > > > ```php > > > > > $a =3D 1; > > $b =3D 2; > > $c =3D 3; > > > > include __DIR__.'/without_import.php'; > > include __DIR__.'/all_import.php'; > > include __DIR__.'/none_import.php'; > > include __DIR__.'/some_vars.php'; > > include __DIR__.'/global_in_function.php'; > > ``` > > > > ## without_import.php > > > > ```php > > > > > echo $a; //1 > > echo $b; //2 > > $c =3D 'any value'; //replace value in global var > > ``` > > > > ## all import.php > > > > ```php > > > > > use vars all; > > > > echo $a; //1 > > echo $b; //2 > > $c =3D 'other value'; //replace value in global var > > ``` > > > > ## none_import.php > > > > ```php > > > > > use vars none; > > > > echo $a; //Warning: undefined var $a > > echo $b; //Warning: undefined var $b > > $c =3D 'other value'; //assign value to local var > > ``` > > > > > > ## some_vars.php > > > > ```php > > > > > use vars $a, $c, $d; //Warning: undefined var $d > > > > echo $a; //1 > > echo $b; //Warning: undefined var $b > > $c =3D 'a value'; //replace value in global var > > ``` > > > > ## global_in_function.php > > > > ```php > > > > > use vars $a, $c; > > > > > > function hello() { > > global $a, $b, $c; > > > > echo $a; //1 > > echo $b; //null. > > $c =3D 'end value'; //replace value in global var > > } > > > > hello(); > > ``` > > > > > > In a project with a lot of global vars( like WordPress ) this > > functionality avoids conflicts and easy replacements of values in main > > global vars. > > >=20 > This looks like what IIFE do already (you can use them today): >=20 > ```php >=20 > require 'something/with/side/effects.php'; >=20 > (static function () use ($a, $b, & $c) : void { > echo $a; //1 > echo $b; //Warning: undefined var $b > $c =3D 'a value'; //replace value in global var > })(); > ``` >=20 > You can use this right now, and it has the advantage of reporting any > missing symbols right at the time at which the closure is declared. >=20 > I tend to use IIFE all the time in procedural code, since they prevent > leaking state into globals =F0=9F=91=8D >=20 > > >=20