php 如何像取消变量一样取消设置函数定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8691884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:16:52  来源:igfitidea点击:

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

php

提问by Vin

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?

就像这样,如果我声明一个函数callMethod(),有没有办法取消它?

回答by Wesley Murch

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

从 PHP 5.3 开始,您可以将匿名函数分配给变量,然后取消设置:

$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()

在 5.3 之前,你可以做类似的事情 create_function()

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

$func2 = "
<?php
rename_function('original_name', 'new_name' );
?>
lambda_1"; echo $func2('test2.a'), "\n"; // Same results, this is the "unset" $func function echo $func('test2.b'); // Fatal Error

回答by realmag777

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

runkit_function_remove删除一个函数定义http://php.net/manual/en/function.runkit-function-remove.php

回答by JG Estiot

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

一种此类功能可以是脚本或框架的移动/平板电脑检测功能。它只需要一次,并且对于只运行一次的函数可能会占用大量内存。如果您有任何疑问,请查看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.

这就是说,有一种方法可以解决该问题,而无需删除该函数。为了它的价值,将函数使用的所有数组设为全局,然后在函数结束时将它们全部取消设置。该功能保持不变,但庞大的数据消失了。请注意,当您取消设置变量时,由 PHP 的垃圾收集器决定何时释放内存。释放内存会占用应用程序的 CPU 周期。我真的看不到任何需要取消设置功能的 PHP 脚本。

回答by M.Babcock

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.

您可以使用 rename_function

function function1(){
 echo "1";
}

function function2(){
 echo "2";
}

$i = "function2";
$i(); //  displays 2;

然后重新声明原始函数。
override_function 也可以工作。

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

它们是 Zend PHP Debugger 的一部分,如果这不起作用,那么您可以随时尝试以下操作:

##代码##

回答by Pete - iCalculator

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
  1. 将您的函数放入单独的文件中
  2. 使用 require_once 调用带有函数的文件

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.

这样,如果您循环遍历代码,就不会遇到两次调用同一个函数的问题。我发现这是一个更简洁的解决方案,它更容易调用。然后,您可以使用 url 扩展来调用函数或类组。

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

显然,这并不能满足删除、取消设置或销毁函数的请求,但它提供了一种可能被证明有用的替代方法。