Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121856 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98627 invoked from network); 29 Nov 2023 09:08:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Nov 2023 09:08:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4903D180043 for ; Wed, 29 Nov 2023 01:08:38 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 29 Nov 2023 01:08:38 -0800 (PST) Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3b8382b8f5aso3958536b6e.0 for ; Wed, 29 Nov 2023 01:08:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701248909; x=1701853709; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5XK9M8s91S5AyBHjS9zOI1GQ5FPkt/m9nJTWSpO1vsI=; b=IcjgUH6yjjZZoX5pXp0AxwBM4vwaC9x1ezCJW3rgZvxqCC0lcphp/+gzyOKC//BzCR hT1T0/zTo+u7cwBtOgAK0z8RPYSbSs/ECa17IrX0gL/8i9Rhdp2uw5I6q337Lmgd1G1d ApGhostb9RQiTCdEUHpMyq8Ovb2RZ3krcpZO5Cf4J5iNBbdbLPvkB7j8W/ETI/7Zk5aQ paMoUflFdakJFYf00kK14962B0kl17sMEc9Pc6D6e/bN9MHJn79IDeRjop7PFUylmIQK HA4ovJJPgQtjbC/DqvQhHBYdN6sU0tB3LGFwYpUJTph8i0wxgLh+m7nsd9/HhXVsbr1B gkpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701248909; x=1701853709; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5XK9M8s91S5AyBHjS9zOI1GQ5FPkt/m9nJTWSpO1vsI=; b=hPnR5rlNsWUkR6+3UylBIgELwraCxcqQVT33XDmdPqwTLVKCBK5uMP4iFCgrMsetXe pJVaWbQUSV3Yv3H2NG6dWmEI+O/1eQjEFw/L0V2b3IB7+YGj4G4MFwPJUSq9cGTrWmXA AXaUu6es7bcgSjEodbVDNo4p7Z7aBxtwkfRz+z9lr5c30hkJ4UzHJd1K1iXrvdzYoXsj SoSLYnquTEHg5pP8o8Cu3IUB2SvXG4PGhNtKBdlBQOnQlJCgkDKhdj4t9mh5IELGsxir hLS4UQzOXE/hHJ2ZxQqVmpyB2gPvq7urNG0V6ytbOwWjFG2ckJNRHvEVeQmNpr9U8Nd2 h+dw== X-Gm-Message-State: AOJu0YwnrIAWXvrbYeH/Q4C4pXLKwAuslAgZ9i5sABSGi2HJw/8jcN2X XVqOcHEpTsHgfL1uUBrGOUQdwdecicmQhP7jZq8= X-Google-Smtp-Source: AGHT+IFEzUv3yN+m50LJFGFftPExkB2mBciR/XalIVdGnFFW5Zhg6Z3vOv5tALgqXDz3JL3sYYihP4HJA11FTPQVxLg= X-Received: by 2002:a05:6870:9120:b0:1f9:eb8b:fda6 with SMTP id o32-20020a056870912000b001f9eb8bfda6mr21285560oae.40.1701248909358; Wed, 29 Nov 2023 01:08:29 -0800 (PST) MIME-Version: 1.0 References: <6566989F.7010305@adviesenzo.nl> <34dada8e-7f2a-4d94-b7df-d9d3c7b2f3ce@app.fastmail.com> In-Reply-To: Date: Wed, 29 Nov 2023 10:08:18 +0100 Message-ID: To: Lynn Cc: Stephen Reay , php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] What is the prevailing sentiment about extract() and compact() ? From: landers.robert@gmail.com (Robert Landers) On Wed, Nov 29, 2023 at 9:56=E2=80=AFAM Lynn wrote: > > > > On Wed, Nov 29, 2023 at 9:20=E2=80=AFAM Robert Landers wrote: >> >> On Wed, Nov 29, 2023 at 8:19=E2=80=AFAM Stephen Reay wrote: >> > >> > >> > >> > > On 29 Nov 2023, at 09:58, Larry Garfield wr= ote: >> > > >> > > On Tue, Nov 28, 2023, at 7:49 PM, Juliette Reinders Folmer wrote: >> > >> L.S., >> > >> >> > >> What with all the drives towards cleaner code, how do people feel >> > >> nowadays about `extract()` and `compact()` still being supported ? >> > >> >> > >> Both have alternatives. The alternatives may be a little more cumbe= rsome >> > >> to type, but also make the code more descriptive, lessens the risk = of >> > >> variable name collisions (though this can be handled via the $flags= in >> > >> extract), prevents surprises when a non-associative key would be >> > >> included in an array and lessens security risks when used on untrus= ted data >> > > >> > > *snip* >> > > >> > >> I can imagine these could be candidates for deprecation ? Or limite= d >> > >> deprecation - only when used in the global namespace ? >> > >> >> > >> For now, I'm just wondering how people feel about these functions. >> > >> >> > >> Smile, >> > >> Juliette >> > > >> > > extract() has very limited use in some kinds of template engine, whi= ch use PHP require() as a template mechanism. I don't think compact() has = any uses. >> > > >> > > I very recently was just reminded that these even exist, as i had to= tell one of my developers to not use them. I think it was compact() he wa= s trying to use. I vetoed it. >> > > >> > > I would not mind if they were removed, but I don't know how large th= e BC impact would be. They'd probably need a long deprecation period, just= to be safe. >> > > >> > > --Larry Garfield >> > > >> > > -- >> > > PHP Internals - PHP Runtime Development Mailing List >> > > To unsubscribe, visit: https://www.php.net/unsub.php >> > > >> > >> > Hi, >> > >> > While I think I understand the goal behind this, I think you're missin= g some factors here. >> > >> > Regarding use-cases for compact: the most common one I can think of fr= om my work, is for passing multiple local variables as context to a logging= function, but I'd be surprised if its not also used to build faux hash str= uctures too. >> > >> > If your goal is to achieve an associative array (i.e a poor mans hash)= of known variable names, using compact in php8+ has *less* risk of uncaugh= t/unexpected errors than building it manually. Passing an undefined name (i= .e. due a typo, or it just not being defined) produces a warning regardless= of whether you build the array manually or pass the name(s) to compact(). = Providing an array key name that doesn't match the variable name (e.g. due = to a typo, or a variable being renamed) will produce no error when building= the array manually, but will produce a warning with compact(). >> > >> > IDEs (e.g. PHPStorm/IDEA+PHP plugin) can already understand that the n= ames passed to compact are a variable name, and make changes when a variabl= e is renamed via the IDE. They simply cannot do the same for plain array ke= ys. >> > >> > Due to how variable scope works, the only way to re-implement compact(= ) with the same key-typo-catching behaviour as a function in userland would= be something that requires the user to pass the result of get_defined_vars= () to every call. >> > >> > So no, I don't think compact() should be deprecated, what I think *sho= uld* happen, is to promote the current warning on undefined variables, to a= n error, as per https://wiki.php.net/rfc/undefined_variable_error_promotion= . Whether this is a foregone conclusion or not, I don't know because that R= FC doesn't mention compact() specifically. >> > >> > >> > extract(), as Larry points out has historically been used by 'pure php= ' style template systems, in a manner that's generally "safe". Personally I= 'm less inclined to use this behaviour now (i.e. I'd prefer to access named= & typed properties from a template than arbitrary local variable names) bu= t I don't think that's enough of a case to remove it, because just like wit= h compact, by nature of how variable scope works, it's very difficult/impos= sible to re-implement this in userland, in a way that's reusable and doesn'= t involve using worse constructs (e.g. eval'ing the result of a function) >> > >> > I think there's possibly an argument to be made for improvements, such= as changing the default mode of extract to something besides EXTR_OVERWRIT= E, or to have checks in place preventing the overwrite of superglobals. >> > >> > >> > Cheers >> > >> > >> > Stephen >> >> FWIW, I use compact all the time, usually like this: >> >> try { >> // do stuff >> } catch(Throwable $exception) { >> $this->logger->error("failed to do stuff", compact('exception')); >> throw $exception; >> } >> >> But thanks for the reminder to finish the nameof RFC, I was waiting >> until after 8.3 to avoid the "trying to rush it to get into 8.3" >> shenanigans that happened to another RFC around the same time. If >> nameof passes, then it could make this more obvious when refactoring: >> >> try { >> // do stuff >> } catch(Throwable $exception) { >> $this->logger->error("failed to do stuff", compact(nameof($exception))= ); >> } >> >> Robert Landers >> Software Engineer >> Utrecht NL >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: https://www.php.net/unsub.php >> > > My main concern with compact and extract is the counterintuitive usage of= variable names, which is also why I've personally broken production after = changing a variable name. If there is another way of using compact in a way= where it's used without string and with dollar sign, I won't have a proble= m with it. I still find `compact(nameof($exception))` a little confusing pe= rsonally though. I have the feeling that the wish is to have a nice syntact= ic sugar for compact, not sure about extract though. Extract for me is some= what of a security concern as it will make it easy to implicitly overwrite = variables from a local scope if the array being used is filled elsewhere. S= omeone adding an array key in another layer of code in an application can c= ause different behavior on a totally unrelated place when there's a variabl= e collision, and there's no way to detect this when adding (or removing) a = key from the array. > > I'm all for getting rid of extract. For compact I'd like to explore alter= native solutions before outright deprecating. I'm still in favor of seeing = it gone in its current form though. It would be neat to combine something like nameof into something more intui= tive: $name =3D "Robert"; $title =3D "Software Engineer"; $arr =3D [ $name =3D> ..., $title =3D> ... ]; // $arr =3D=3D ['name' =3D> 'Robert', 'title' =3D> 'Software Engineer'] would basically mean take the name of the variable as the key and the value as the value of the array. If we took the same semantics as nameof, then even this would work: $arr =3D [ $this->name =3D> ..., $this->title =3D> ... ]; // $arr =3D=3D ['name' =3D> 'Robert', 'title' =3D> 'Software Engineer'] I'm sure there is probably a prettier way to express it, but I don't dislike it. Then we just deprecate compact. There's already destructuring to handle extract's case: [ 'name' =3D> $name, 'title' =3D> $title ] =3D $arr; Robert Landers Software Engineer Utrecht NL