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?
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?
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
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 runkit_function_remove — Remove a function definition http://php.net/manual/en/function.runkit-function-remove.php
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.
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;
rename_function
is a part of APD package, not available in standard PHP installations. –
Unhallow A solution that avoids the requirement to unset a function and still use traditional 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.
© 2022 - 2024 — McMap. All rights reserved.