> On Jun 16, 2019, at 07:04, Mark Randall <mar...@gmail.com> wrote: > > On 15/06/2019 22:53, Wes wrote: >> Hello PHP, I just published >> https://wiki.php.net/rfc/alternative-closure-use-syntax >> I would love your opinion on this > > I'm not overly fond of it myself because I think it could make it slightly > more difficult to parse in my brain. > > If there was a: > > $x = 0; > $y = 1; > > $closure = function() use (...) { > > }; > > Where "use (...)" would auto-capture all of the used variables in a similar > manner to short closures, that would certainly save a bit of time.
Would this mean that all variables in the “parent" are now available in the “child?” This seems like it could result in some unexpected surprises as scope “leaks” across these boundaries. As others have pointed out, I think there are better ways to solve the problem illustrated in the RFC. With a bit more code, you can use an anonymous object to accomplish the same thing, without any use statements. $anonymousObject = new class( $var1, $var2, $var3, $var4 ) { private $v1, $v2, $v3, $v4; public function __construct( $importVariable1, &$importVariable2, $importVariable3, &$importVariable4 ) { $this->v1 = $importVariable1; $this->v2 = $importVariable2; $this->v3 = $importVariable3; $this->v4 = $importVariable4; } public function __invoke( ArgumentType $argument1, ArgumentType $argument2, ArgumentType $argument3, ArgumentType $argument4 ): ReturnType { // do whatever you need here, accessing $this->v1, etc. } }; However, I would argue that, if you find yourself needing to do something this complex, it’s probably best to move it into its own named class so that it’s easier to test and maintain. -Ben
signature.asc
Description: Message signed with OpenPGP