Skip to content

Instantly share code, notes, and snippets.

@schmengler
Last active October 1, 2024 18:35
Show Gist options
  • 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