Zend certified PHP/Magento developer

Here’s a quick tip: you can use the null coalescing assignment operator for easy memoisation in PHP 7.4

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]