thunderer / Once

once() function experiment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Once

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:

Usage

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']));

License

MIT, of course.

About

once() function experiment

License:MIT License


Languages

Language:PHP 100.0%