Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111392 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31969 invoked from network); 9 Aug 2020 15:59:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Aug 2020 15:59:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4D56B1804D9 for ; Sun, 9 Aug 2020 07:58:54 -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,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,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 mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) (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, 9 Aug 2020 07:58:53 -0700 (PDT) Received: by mail-ot1-f51.google.com with SMTP id q9so5341036oth.5 for ; Sun, 09 Aug 2020 07:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wScbUdAVTFGLQEf53z8TOZidBPnXQXMxXD6DvUdYZr8=; b=Tz4VeSMjwOX45I0SoN8DFtGzumaiTnqFpi/Fd2kFQvVi500Asc/jZ7ZKGKd+XfNbVP ISdsnR99+QLoKjYT25L5FYaIjWV+YVD03Gl/SSNtuvfOmVyPxwxkymHwxo26EhbzflOo ANFtH2R+YGXXHswejUtyA81Si9eauKhkqCDHXhXUMf9EwO5JYPN/PSvi4tBUqK+TBBh3 aHFN3a+SHDoJfMhq0Z08TnGRie0LeAjYJkIASc/y5hm41acoqcBHxRZFzIieuFLPUToD 86kSYu4AVZQbcXfV/37bSNdX01lRWjixEpwFsBMLKtpPSmPi1fNFjTZJmT56ehiyp++3 wg5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wScbUdAVTFGLQEf53z8TOZidBPnXQXMxXD6DvUdYZr8=; b=QzDnoMpNoIu9TDPXXVuLKrYeuXPZZvFYyHd1BmqU9aoPYbnbb86MoQMj4RsbpvdQix UnGf5cwlTrFJ32sSKyvMOZtVVdkQOJCLuGY/GZKX8FpHPSYFQYNseO9Vd+HBaDtqPUqM ekGOxHUdZojxU5BZI5y+IghD+/PeH5zSTYLqDVIsx21pDgb/CZzZ9I0tqP01fEgyV4FX vNcq/KurI88ewAowWm+Ao2ceJ0r6ZPz3tXBxmVudCvq7PPlVwgt/9N3epMvVUY/df+Sa aGc5nv7OdfpHFTTiwhJ2LtXqi8RiO2xjRYrcuEWbdZlQJFeOclnX/ad2sGAwDkat9xL8 sBsA== X-Gm-Message-State: AOAM533kivvQRfh0usZyDXXkx0QLaXWcaGCVPzeMCMGB3VzCaR7rb1U+ 2JlAJTSvHJog97Xt0iWikKfTxgHIgPBzLpdn4Gg= X-Google-Smtp-Source: ABdhPJxs3k9/liBb7i1NwKrh9K2s5sPIlXye5HWtC4a1vjLZFabeS6tNGiyo3q3ppJfuBGerlwFwK6T47IhLyK6a+ss= X-Received: by 2002:a05:6830:4001:: with SMTP id h1mr19410359ots.219.1596985133145; Sun, 09 Aug 2020 07:58:53 -0700 (PDT) MIME-Version: 1.0 References: <173d2736435.eeebc066559719.6668730031195302987@manuelcanga.dev> In-Reply-To: Date: Sun, 9 Aug 2020 11:58:41 -0300 Message-ID: To: tyson andre Cc: Manuel Canga , Internals Content-Type: multipart/alternative; boundary="00000000000092a33a05ac7313e2" Subject: Re: [PHP-DEV] [RFC] Import of variables From: david.proweb@gmail.com (David Rodrigues) --00000000000092a33a05ac7313e2 Content-Type: text/plain; charset="UTF-8" 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. Or, to avoid create more complexity and possible new keywords to import syntax, maybe create a new function: importx(string $file, ?array $extract = null) and requirex(...) (importx and requirex is only pseudo) Em dom, 9 de ago de 2020 11:42, tyson andre escreveu: > Hi Internals, > > > 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 ). > > 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(). > > It may be possible to use a declare syntax, e.g. > declare(used_variables='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. > > I can see this as having some use cases, such as in configuration files or > files used for bootstrapping. > For example, > > ``` > declare(used_variables=null); > > $api_base = IS_PRODUCTION ? 'https://example.com/api/' : ' > http://localhost/api'; > do_stuff(); > > return [ > // long config array > 'url_new' => "$api_base/new", > 'url_all' => "$api_base/all", > ]; > ``` > > 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. > > 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). > > 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 = []) { 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. > > Regards, > - Tyson > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --00000000000092a33a05ac7313e2--