A small function for memorize a heavy computed value
composer require hyqo/memorize
Without memorize
, each call getValue()
method will return a new $counter
value, nothing changes:
class Foo() {
private $counter = 0;
public function getValue(): int
{
return $this->counter++;
}
};
$foo = new Foo();
$foo->getValue(); // 0
$foo->getValue(); // 1
$foo->getValue(); // 2
With memorize
, each call will return the first calculated value:
use function Hyqo\Memorize\memorize;
class Foo() {
private $counter = 0;
public function getValue(): int
{
return memorize(function () {
return $this->counter++;
});
}
};
$foo = new Foo();
$foo->getValue(); // 0
$foo->getValue(); // 0
$foo->getValue(); // 0