How to unset a function definition just like we unset a variable?
Asked Answered
G

5

31

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?

Gid answered 1/1, 2012 at 4:48 Comment(5)
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.Huzzah
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.Gid
Why, just for fun? Whatever your actual problem is here, it sounds like you may be forcing the wrong solution.Sixty
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.Gid
@Forte - So what do you expect to happen if the function is referenced after it 'vanishes'? What is the practical use case?Boding
S
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
Sixty answered 1/1, 2012 at 5:2 Comment(2)
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.Vulvitis
There is one more answer further down.Cordie
A
18

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

Arda answered 30/10, 2015 at 14:35 Comment(4)
The only answer here. Why are the others getting upvoted ?Cordie
This requires PECL so isn't out of the bag PHP. Good function though.Vendue
Correct. This is only way.Braynard
As of PHP7 this is now called runkit7_function_removeChronopher
B
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.

Boom answered 3/6, 2014 at 10:23 Comment(1)
what about the answer above you? is that enough of a reason? i also had that problem before.Whitehouse
B
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;
Boding answered 1/1, 2012 at 5:0 Comment(4)
Yeah but he wants to get rid of the function altogether.Huzzah
@Huzzah - I'll agree with your comment above. How is that practical?Boding
rename_function is a part of APD package, not available in standard PHP installations.Unhallow
This does not answer the question.Cordie
P
-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.

Placement answered 28/2, 2020 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.