class CacheTest { private $cache = []; public function set($key, $value) { $this->cache[$key] = $value; } public function getOrNull($key) { return $this->cache[$key] ?? null; } public function getOrCall($key, $callback) { if (isset($this->cache[$key])) { return $this->cache[$key]; } else { return $this->cache[$key] = $callback(); } } } $cache = new CacheTest(); [$str1, $str2] = ["alive", "dead"]; $value1 = $cache->getOrCall('mordor', function() use ($str1) { return "Mordor is $str1!"; }); $value2 = $cache->getOrCall('mordor', function() use ($str2) { return "Mordor is $str2!"; }); header('Content-Type: text/plain; charset=UTF-8'); print_r([$value1, $value2]);