How to unset a function definition just like we unset a variable?

As of PHP 5.3, you can assign an anonymous function to a variable, then unset it:

$upper = function($str) {
    return strtoupper($str);
};

echo $upper('test1');
// outputs: TEST1

unset($upper);

echo $upper('test2');
// Notice: Undefined variable: upper
// Fatal error: Function name must be a string

Before 5.3, you can do something similar with create_function()

$func = create_function('$arg', 'return strtoupper($arg);');
echo $func('test1');
unset($func);

$func2 = "\0lambda_1";
echo $func2('test2.a'), "\n"; // Same results, this is the "unset" $func function

echo $func('test2.b'); // Fatal Error

runkit_function_removeRemove a function definition http://php.net/manual/en/function.runkit-function-remove.php

Tags:

Php