PHP program to call a given function only once.

bookmark

<?php
//Licence: https://bit.ly/2CFA5XY

function once($function)
{
    return function (...$args) use ($function) {
        static $called = false;
        if ($called) {
            return;
        }
        $called = true;
        return $function(...$args);
    };
}

$add = function ($a, $b) {
    return $a + $b;
};

$once = once($add);

var_dump($once(10, 5));  
var_dump($once(20, 10));
  
?>


Output:

int(15)
NULL