Re: What is the prevailing sentiment about extract() and compact() ?

From: Date: Wed, 29 Nov 2023 09:02:20 +0000
Subject: Re: What is the prevailing sentiment about extract() and compact() ?
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-121855@lists.php.net to get a copy of this message
Robert Landers Software Engineer Utrecht NL On Wed, Nov 29, 2023 at 9:36 AM Ayesh Karunaratne <ayesh@php.watch> wrote: > > > > > try { > > // do stuff > > } catch(Throwable $exception) { > > $this->logger->error("failed to do stuff", > > compact('exception')); > > throw $exception; > > } > > > > I wonder why not just create an array with the key... > > ```php > try { > // do stuff > } catch(Throwable $exception) { > $this->logger->error("failed to do stuff", ['exception' => > $exception])); > throw $exception; > } > ``` > > It's a few more characters of course, but I would pick the readability > and simplicity over the potential typos (which the IDE should point > out) and a few key presses it saves.I Hey Ayesh, Yeah, this is very much a personal preference kind of thing. I choose compact not because it is shorter to type, but because typos are a thing if you've been around PHP for awhile... who hasn't opened a function written 15 years ago with a misspelled variable that you want to fix but it has been that way for 15 years, so you don't fix it. I've also used compact inside array_maps like: $arr = array_map(function($a) { $name = $a['first'] . " " . $a['last']; // more mappings return compact('name', ...); }, $arr); just because it is easier to type/read than a bunch of array accesses. Possibly faster as well, but if it is, it's by nano-seconds. Robert Landers Software Engineer Utrecht NL

« previous php.internals (#121855) next »