28

I want to define a function and unset it after its use just like we do with variables.

$a = 'something';
unset($a);
echo $a; // outputs nothing

Just like this if i declare a function callMethod(), is there a way to unset it?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Vinay Jeurkar
  • 3,054
  • 9
  • 37
  • 56
  • 3
    Why do you want to do this? Also what does this have to do with functional programming? I would think that disposable functions go outright against FP. – BoltClock Jan 01 '12 at 04:53
  • All i want is that get_defined_functions()'s output should not contain the name of the function 'callMethod'. If there is any other way to do that please let me know. – Vinay Jeurkar Jan 01 '12 at 05:07
  • Why, just for fun? Whatever your actual problem is here, it *sounds* like you may be forcing the wrong solution. – Wesley Murch Jan 01 '12 at 05:14
  • I just want to create a function, use it several times and then vanish it so that no other php codes will be able to access it. Thats all my requirement is. – Vinay Jeurkar Jan 01 '12 at 05:25
  • 1
    @Forte - So what do you expect to happen if the function is referenced after it 'vanishes'? What is the practical use case? – M.Babcock Jan 01 '12 at 05:57

5 Answers5

31

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
hakre
  • 193,403
  • 52
  • 435
  • 836
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • 1
    Functions created with `create_function` will stay in memory. If you output the variable, you will see a string. If you use that string later on, you will see that the function still works albeit you had unset the variable. So I'd say `create_function` simulates what you want but is not what you want. You could use variable functions as well, that's the same. – hakre Aug 01 '12 at 08:04
  • There is one more answer further down. – Rohit Gupta Nov 14 '15 at 03:24
18

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

realmag777
  • 2,050
  • 1
  • 24
  • 22
2

For the doubters, it makes perfect sense to want to delete a function. If you have a function that uses a lot of data just once at the beginning of your script then wanting to delete it makes sense.

One such type of function may be the mobile/tablet detection function of a script or framework. It is only needed once and it is likely to take a significant amount of memory for a function that runs only once. If you have any doubt, check https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.json

This said, there is a way around the problem without needing to delete the function. For what it is worth, make global all the arrays used by the function then at the end of the function unset them all. The function stays but the bulky data goes. Note that when you unset a variable, it is up to PHP's garbage collector to decide when the memory will be freed. And freeing the memory takes CPU cycles away from your application. I really cannot see any PHP script that would require unsetting a function.

JG Estiot
  • 969
  • 1
  • 10
  • 8
  • what about the [answer](http://stackoverflow.com/a/16924339/1057527) above you? is that enough of a reason? i also had that problem before. – machineaddict May 20 '15 at 08:34
1

From this question:

You can use rename_function

<?php
rename_function('original_name', 'new_name' );
?>

and then redeclare the original function.
override_function may work too.

They are part of the Zend PHP Debugger, if that doesn't work then you can always try something like:

function function1(){
 echo "1";
}

function function2(){
 echo "2";
}

$i = "function2";
$i(); //  displays 2;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
-1

A solution that avoids the requirement to unset a function and still use traditional functions....

  1. Place your function into a separate file
  2. Use require_once to call the file with the functions

This way, if you are looping through code, you wont get an issue with calling the same function twice. I find this a neater solution and it makes it easier to call. you can then use url extensions to call groups of function or classes.

Clearly this doesn't answer the request to remove, unset or destroy a function but it provides an alternate approach which may prove useful.

Pete - iCalculator
  • 929
  • 1
  • 8
  • 12