Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111403 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64422 invoked from network); 9 Aug 2020 20:37:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Aug 2020 20:37:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C96421804D9 for ; Sun, 9 Aug 2020 12:36:42 -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_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from brown.birch.relay.mailchannels.net (brown.birch.relay.mailchannels.net [23.83.209.23]) (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 12:36:36 -0700 (PDT) X-Sender-Id: dreamhost|x-authsender|josh@joshbruce.dev Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id E7B55219A9; Sun, 9 Aug 2020 19:36:34 +0000 (UTC) Received: from pdx1-sub0-mail-a19.g.dreamhost.com (100-96-8-55.trex.outbound.svc.cluster.local [100.96.8.55]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 3BDB6219AB; Sun, 9 Aug 2020 19:36:34 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|josh@joshbruce.dev Received: from pdx1-sub0-mail-a19.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.18.8); Sun, 09 Aug 2020 19:36:34 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|josh@joshbruce.dev X-MailChannels-Auth-Id: dreamhost X-Eight-Army: 7f52abd515ad1d75_1597001794708_2961394516 X-MC-Loop-Signature: 1597001794708:3594370035 X-MC-Ingress-Time: 1597001794708 Received: from pdx1-sub0-mail-a19.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a19.g.dreamhost.com (Postfix) with ESMTP id E173B7F5A1; Sun, 9 Aug 2020 12:36:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=joshbruce.dev; h= content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s= joshbruce.dev; bh=6/39HrjfTxdstR1yGijTedAjvP0=; b=gj9zipjId33dBZ QPRDNpAMgKrPhWpeM03XV+YES7qwcvsCf8NYgkPljLsVN+hGXg25UJA6mJVmCw5K HYz8Y4RJkQY4O0UIzheqXj41tJPCjflQLn1c7MyltJB7TdUQ3p1niCZapkTje1jj 3xje/5mC7FSckHhbQM0R/V3jO3pOw= Received: from joshs-mbp.lan (21.140.29.136.in-addr.arpa [136.29.140.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: josh@joshbruce.dev) by pdx1-sub0-mail-a19.g.dreamhost.com (Postfix) with ESMTPSA id 3C2BA7F598; Sun, 9 Aug 2020 12:36:32 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.1\)) X-DH-BACKEND: pdx1-sub0-mail-a19 In-Reply-To: Date: Sun, 9 Aug 2020 14:36:30 -0500 Cc: tyson andre , Manuel Canga , Internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <173d2736435.eeebc066559719.6668730031195302987@manuelcanga.dev> To: David Rodrigues X-Mailer: Apple Mail (2.3608.120.23.2.1) X-VR-OUT-STATUS: OK X-VR-OUT-SCORE: -100 X-VR-OUT-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduiedrkeeigddufeekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuggftfghnshhusghstghrihgsvgdpffftgfetoffjqffuvfenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurheptggguffhjgffgffkfhfvofesthhqmhdthhdtvdenucfhrhhomheplfhoshhhuceurhhutggvuceojhhoshhhsehjohhshhgsrhhutggvrdguvghvqeenucggtffrrghtthgvrhhnpeejgeeghfeiieekhfdtkeefvddtjeejvdeitedtveevgeeiudeiteejueegieduudenucffohhmrghinhepvgigrghmphhlvgdrtghomhdpphhhphdrnhgvthenucfkphepudefiedrvdelrddugedtrddvudenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhhouggvpehsmhhtphdphhgvlhhopehjohhshhhsqdhmsghprdhlrghnpdhinhgvthepudefiedrvdelrddugedtrddvuddprhgvthhurhhnqdhprghthheplfhoshhhuceurhhutggvuceojhhoshhhsehjohhshhgsrhhutggvrdguvghvqedpmhgrihhlfhhrohhmpehjohhshhesjhhoshhhsghruhgtvgdruggvvhdpnhhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth Subject: Re: [PHP-DEV] [RFC] Import of variables From: josh@joshbruce.dev (Josh Bruce) Have not tested - just catching up on emails. To verify: A property defined with a default value of the global does = not already do this? Developer need: I rarely access global variables directly - usually = going through a lib or framework. When I do I would probably wrap the = access in a trait or method on a class. Cheers, Josh > On Aug 9, 2020, at 9:58 AM, David Rodrigues = wrote: >=20 > I suggests to make something like Node does: import "file.php" ($a, = $b) so > $a and $b must be extracted from file.php context. All other variables > should be ignored. >=20 > Or, to avoid create more complexity and possible new keywords to = import > syntax, maybe create a new function: >=20 > importx(string $file, ?array $extract =3D null) and requirex(...) = (importx > and requirex is only pseudo) >=20 > Em dom, 9 de ago de 2020 11:42, tyson andre = > escreveu: >=20 >> Hi Internals, >>=20 >>> 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 ). >>=20 >> To be clear: The variables in the top-level scope depend on what has >> require()d/include()d a file. >> The top-level scope starts off as being global, but if a file is = required >> from within a function/method/closure (e.g. the autoloader closure), = then >> the top-level scope in the require()'d file uses variables (e.g. = $this) >> from whatever context called require(). >>=20 >> It may be possible to use a declare syntax, e.g. >> declare(used_variables=3D'all') for `'all'`, `null`, `['var1', = 'var2']`, etc. >> - Otherwise, you face the issue of where `use vars` should be = allowed, >> what happens if there's a statement before `use vars`, etc. >>=20 >> I can see this as having some use cases, such as in configuration = files or >> files used for bootstrapping. >> For example, >>=20 >> ``` >> > declare(used_variables=3Dnull); >>=20 >> $api_base =3D IS_PRODUCTION ? 'https://example.com/api/' : ' >> http://localhost/api'; >> do_stuff(); >>=20 >> return [ >> // long config array >> 'url_new' =3D> "$api_base/new", >> 'url_all' =3D> "$api_base/all", >> ]; >> ``` >>=20 >> This feature (ignoring the question of syntax) would ensure that = people >> reading the file knew that $api_base was not modified by other files >> and that other files did not read local variables created within a >> configuration/bootstrapping file in unexpected ways, >> which is a fairly common issue in some web apps I've worked on. >> Opcache would also do a better job at optimizing code if it knew = which >> variables in a top-level scope couldn't be modified. >>=20 >> That being said, there's been opposition to extensions to the = language >> that add functionality that can be implemented in other ways, as in = Rowan's >> comment, >> but peoples opinions depend on the specifics of the proposal >> (e.g. `match` was added and was more performant than chained = conditionals >> or switch). >>=20 >> As Rowan said, there are ways to reimplement this: >> - Wrapping the config file or bootstrapping file in a closure, global >> function, or class method >> - `function safe_require_once(string $path, $vars =3D []) { = extract($vars); >> require($path); }` from the caller, to limit what variables are = passed in. >> IDEs/tooling would be worse at telling you if a file name had a typo, >> though. >>=20 >> Regards, >> - Tyson >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: https://www.php.net/unsub.php >>=20 >>=20