Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111407 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 76091 invoked from network); 9 Aug 2020 22:59:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Aug 2020 22:59:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8211C1804D9 for ; Sun, 9 Aug 2020 14:58:01 -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 14:58:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597010278; cv=none; d=zohomail.com; s=zohoarc; b=dMGmuiKCvNqwLsRWYj3DEaA1KX0KNNE0dedOhRmxXgzU4CUAXNl6IlNp1uMJrVa+QJ4prnnrV0+Ts7nJ3oJSjYrimgEb+MKb3Z35N5gcDFllAX7CR1QrJosiyzQ7eWHojGQ270BJNcZ6pma+tBKmZWkd3mMk9LzUMU8Q0rwC6eU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597010278; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=80SCkTXapesrs/gL3C/56SsxnxxpjZS/KFZ6OnSNfMM=; b=FOHfbJRBV31OQH1qH1j72RHJJalkszZMx79AnGT9VpEN1UWi/SC+VMzrnpA4q5c2/qYcRq37BrXmiVRmgteXXCwFw2nouygHqmhS/amIG1txPqh9QWhhs+/JzbqQwYlW5LJHCA/gEA7BmtekSWFeOaIfZcYOi7dIjuRidK1BF+o= 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=1597010278; 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=80SCkTXapesrs/gL3C/56SsxnxxpjZS/KFZ6OnSNfMM=; b=NyMmLwuCw48+ynwqxl+vvs9AZlWyjaAsvj6W+j6N8odxtNsyvPCWH11b/GjzywPJ 1NgG5nm5d1sP5O38m2amTjbR86Z1k0EKSB1gBOWpiCO455g2Sd3g7LB29pkeek3ZRB6 hjsnbWo1hyQrXehoI9APzmwc3y41+hUxAF6DwLEw= Received: from mail.zoho.com by mx.zohomail.com with SMTP id 1597010246919455.35531490616984; Sun, 9 Aug 2020 14:57:26 -0700 (PDT) Date: Sun, 09 Aug 2020 23:57:26 +0200 To: "tyson andre" Cc: "Internals" Message-ID: <173d5399f3c.e6304d5b580764.6781921355672822606@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, Tyson, Thanks.=20 ---- En dom, 09 ago 2020 16:41:44 +0200 tyson andre escribi=C3=B3 ---- > Hi Internals, >=20 > To be clear: The variables in the top-level scope depend on what has req= uire()d/include()d a file. > The top-level scope starts off as being global, but if a file is require= d 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) fr= om whatever context called require(). My idea was using 'use vars' not only with global vars but also with vars i= n context. > 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, w= hat happens if there's a statement before `use vars`, etc. Good point, I don't think about that.=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://local= host/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 confi= guration/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 v= ariables in a top-level scope couldn't be modified. >=20 > That being said, there's been opposition to extensions to the language t= hat 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 conditional= s or switch). >=20 > As Rowan said, there are ways to reimplement this: > - Wrapping the config file or bootstrapping file in a closure, global fu= nction, or class method > - `function safe_require_once(string $path, $vars =3D []) { extract($var= s); 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, t= hough. I think in views system( of some frameworks ) or CMS like WordPress. In Wo= rdPress, for example, I can do something like this: ## index.php ``` foreach($posts as $post ) { $post =3D 'foo'; } ``` index.php is loaded from CMS. $post is a global variable used in WordPress = core. So that, I am crashing WordPress. However, with: ``` use vars none; foreach($posts as $post ) { $post =3D 'foo'; } ``` I don't crash my WordPress or any external plugin which can use global vars= . My file now is a container( black box ) where my code is isolated. Regards