Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Created March 16, 2023 13:59
Show Gist options
  • Save calebporzio/d847abaed2ad491128352db21f3fac9d to your computer and use it in GitHub Desktop.
Save calebporzio/d847abaed2ad491128352db21f3fac9d to your computer and use it in GitHub Desktop.
<?php
function memoize($target) {
static $memo = new WeakMap;
return new class ($target, $memo) {
function __construct(
protected $target,
protected &$memo,
) {}
function __call($method, $params)
{
$this->memo[$this->target] ??= [];
$signature = $method . crc32(json_encode($params));
return $this->memo[$this->target][$signature]
??= $this->target->$method(...$params);
}
};
}
@calebporzio
Copy link
Author

calebporzio commented Dec 27, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment