This is a small experiment about making a function once()
that makes sure that given callable is called only once.
Remember, DO NOT USE THIS IN ANY PROJECT.
Inspiration:
Closure
$closure = function() {
$range = range('A', 'Z');
return $range[array_rand($range)];
}
assert(once($closure) === once($closure));
Class
$class = new class($range) {
public function foo() {
$range = range('A', 'Z');
return $range[array_rand($range)];
}
};
assert(once([$class, 'foo']) === once([$class, 'foo']));
MIT, of course.