/dev/blog
Bez Hermoso, Software Engineer @ Square
Since PHP 7.0, the addition of the null coalescing operator ??
has made writing certain boilerplate patterns obsolete, like the common pattern of using isset(...)
to avoid a myriad of errors when addressing deeply-nested array elements that don’t exist.
Another pattern that is ripe for replacement is for value re-use, like those usually found in singletons, in-memory caches, memoizations:
<?php
class Singleton
{
private static $instance;
private function __construct() { ... };
public static function getInstance(): Singleton
{
return self::$instance = self::$instance ?? new static();
}
}
<?php
class Fibonacci
{
private $cache = [];
public function find($n): int
{
return $this->cache[$n] =
$this->cache[$n] ?? self::find($n) + self::find($n - 2);
}
}