Skip to content

Instantly share code, notes, and snippets.

@schmengler
Last active June 20, 2023 21:36
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmengler/79a63e82980c9ad008ab280dcd33a03f to your computer and use it in GitHub Desktop.
Save schmengler/79a63e82980c9ad008ab280dcd33a03f to your computer and use it in GitHub Desktop.
<?php
trait Memoize
{
/**
* @var array [method][parameters]
*/
private $memoizedResults = [];
protected function memoizedCall($methodName, $args)
{
$serializedArgs = \serialize($args);
if (! isset($this->memoizedResults[$methodName][$serializedArgs])) {
$this->memoizedResults[$methodName][$serializedArgs] =
$this->subject->$methodName(...$args);
}
return $this->memoizedResults[$methodName][$serializedArgs];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment