-
-
Save ricardov03/e08edb6b9b9dd77aa9382ee1cc1ee8e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment