I’m sure this is something most of us have done before to cache the result of an expensive function call:
if (! isset($this->cache[$input])) { $this->cache[$input] = $this->resolve($input); } return $this->cache[$input];
With the null coalescing assignment operator, you can write it like this:
return $this->cache[$input] ??= $this->resolve($input);
Here are a couple of screenshots with a more complete example.
submitted by /u/brendt_gd
[link] [comments]