Re: First class callable syntax for instance methods

From: Date: Thu, 13 Apr 2023 15:12:08 +0000
Subject: Re: First class callable syntax for instance methods
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-120030@lists.php.net to get a copy of this message
> I cannot really assess how big of a hassle it would be, but what I personally think that it > look nice is something like this: > > ``` > class Verification > { > public function __construct(private string $id) {} > > public function getId(): string > { > return $this->id; > } > > public static function getIdAbs(self $instance) { > return abs($instance->id); > } > } > > $items = [new Verification(“1”), new Verification(“2”))]; > > array_map($$->getId(…), $items); > > array_map($$::getIdAbs(…), $items); > ``` > > This way the class does not have to be imported. Later one might be able to add additional > parameters even. > > ``` > class Verification > { > // […] > > public static function getIdAbs(self $instance, array $idMap) { > // do something with the $idMap > return abs($instance->id); > } > } > > // […] > > array_map($$::getIdAbs(…, $idMap), $items); > > ``` > > I know this might be a little more exotic. But it is very straight forward for static analysis > tools. And no import has to be done for classes. So right after I sent this, I just realised how significantly different it is all I just wrote. But actually using it with the array_map too I think. Because the closure created and the passed to the array_map has no relation to the items in the array passed as a second parameter.

« previous php.internals (#120030) next »