从当前函数中取消设置所有已定义的变量
例如,我有这个函数:
function foo($whaaaat){
$var1 = 'a';
$a = 1;
$b = 2;
...
// here unset all variables defined above (including arguments)
require 'somefile.php';
}
那么,我可以取消设置 require 点之前的所有变量吗?
显然,无需对每个变量手动调用 unset,因为这是我自己想出来的:)
是否有一些函数可以做到这一点?
For example, I have this function:
function foo($whaaaat){
$var1 = 'a';
$a = 1;
$b = 2;
...
// here unset all variables defined above (including arguments)
require 'somefile.php';
}
So, can I unset all those variables before the require point?
Obviously without calling unset manually on each variable, because that I figured out myself :)
Is there some function that does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以将
require
包装在匿名函数中:You can wrap the
require
in an anonymous function:假设这些函数内变量都没有声明为全局变量,您可以尝试
获取局部变量列表,然后循环结果以取消设置它们。
Assuming none of those in-function variables are declared global, you could try something like
to get a list of the local variables, then loop over the results to unset them.
你可以这样做:
You could do it like so:
这可能有效(未经测试):
编辑:正如我所说,从未测试过也从未使用过它,但它可以引导亚历克斯走向正确的方向。
This might work (untested):
Edit: As I said, never tested nor ever used this, but it could lead Alex in the right direction.
这是一个解决方案:
我的测试输出如下:
Here's a solution:
My test outputs this:
没有内置任何内容,但您可以使用 get_defined_vars() 函数获取所有已定义变量的数组,然后使用 foreach 循环取消它们。这样您只需 4 行即可完成所有操作,而且非常灵活。
编辑:正如 Marc B 指出的那样,这也会取消设置您的 $GLOBAL 变量。我编辑了我的示例以展示如何正确获取变量列表。
There's nothing built in, but you can use the get_defined_vars() function to get an array of all defined variables, then use a foreach loop to unset them. You'll get it all done in 4 lines that way, and it's flexible.
Edit: As Marc B pointed out, this will unset your $GLOBAL variables too. I've edited my example to show how to properly get the list of variables.
我同意油石的观点。
get_define_vars()
非常适合“多次取消设置”技巧。下面的方法取消设置所有变量。 除了在 foreach 循环内的无名数组中找到的之外的所有内容。
这些值实际上是不带“$”的变量名称,其中变量
unset($$k);
与 REAL 和定义的变量匹配,并销毁它(如果存在)。因此,通过这种方式,您可以完全控制系统剩下的内容。请注意,某些共享托管设置仅依赖于 _SERVER 超全局变量,因此根本不需要 _ENV、HTTP_SESSION_VARS、HTTP_COOKIE_VARS。最有可能的是,您总是希望保留 _COOKIE 和 _SESSION,但不想保留 _GET 和 _POST 或 _FILES。决定各不相同。在将这个技巧应用到您的生产环境之前,请亲自测试并查看数组中应包含哪些内容。
I agree with Whetstone.
get_defined_vars()
is just perfect for "multi-unset" tricks.This method below, unsets ALL variables. All except those found within nameless array inside foreach loop.
The values are actually variable names without '$', where variable variable
unset($$k);
matches the REAL and defined one, and destroys it, if it is present. So, on this way, You can have complete control over what is left for system.Note that some shared hosting setups are relying only on _SERVER superglobals, therefore _ENV, HTTP_SESSION_VARS, HTTP_COOKIE_VARS are not required at all. Most probably You'll always want to keep _COOKIE and _SESSION but not _GET and _POST as well or _FILES. The decision varies. Test and see for Your self what should stand within array before putting this trick on Your production environment.
需要这个,而这里现有的答案并没有真正的帮助。以下内容很简短,切中要点,并且有效:
如果在此之后调用
var_dump(get_define_vars())
,您将得到一个空数组。注意:除非在全局范围内(问题不是,它在函数中),否则
get_define_vars
不会返回任何全局变量,因此无需过滤或检查任何内容,只需全部取消设置即可。Needed this, and the existing answers here weren't really helpful. The following is short, to the point, and works:
If calling
var_dump(get_defined_vars())
after this, you will get an empty array.Note: Unless in global scope (which the question is not, it's in a function), the
get_defined_vars
won't return any globals, so no need to filter or check anything, just unset it all.