从当前函数中取消设置所有已定义的变量

发布于 2024-12-11 05:29:49 字数 314 浏览 0 评论 0原文

例如,我有这个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

骷髅 2024-12-18 05:29:49

您可以将 require 包装在匿名函数中:

function foo($a){
  $b = 2;

  call_user_func(function() {
   require 'somefile.php';
  });
}

You can wrap the require in an anonymous function:

function foo($a){
  $b = 2;

  call_user_func(function() {
   require 'somefile.php';
  });
}
爱格式化 2024-12-18 05:29:49

假设这些函数内变量都没有声明为全局变量,您可以尝试

array_diff(get_defined_vars(), $GLOBALS)

获取局部变量列表,然后循环结果以取消设置它们。

Assuming none of those in-function variables are declared global, you could try something like

array_diff(get_defined_vars(), $GLOBALS)

to get a list of the local variables, then loop over the results to unset them.

从此见与不见 2024-12-18 05:29:49

你可以这样做:

function require_noscope($filename)
{
    // Required file cannot see variables from other function
    require $filename;
}

function foo($whaaaat){

  $var1 = 'a';  
  $a = 1;
  $b = 2;
  require_noscope('somefile.php');
}

You could do it like so:

function require_noscope($filename)
{
    // Required file cannot see variables from other function
    require $filename;
}

function foo($whaaaat){

  $var1 = 'a';  
  $a = 1;
  $b = 2;
  require_noscope('somefile.php');
}
千仐 2024-12-18 05:29:49

这可能有效(未经测试):

$allVars = get_defined_vars(); // Returns array of all defined variables
foreach ($allVars as $allVars) { unset($allVars); }

编辑:正如我所说,从未测试过也从未使用过它,但它可以引导亚历克斯走向正确的方向。

This might work (untested):

$allVars = get_defined_vars(); // Returns array of all defined variables
foreach ($allVars as $allVars) { unset($allVars); }

Edit: As I said, never tested nor ever used this, but it could lead Alex in the right direction.

梦醒时光 2024-12-18 05:29:49

这是一个解决方案:

echo('Before: ' . $test1 . '<br>');

$vars = array_keys(get_defined_vars());
foreach($vars as $var) {
    if($var == 'GLOBALS' || $var == '_POST' || $var == '_GET' || $var == '_COOKIE' || $var == '_FILES' || $var == '_REQUEST' || $var == '_SERVER' || $var == '_ENV')
        continue;
    unset($var);
}
echo('After: ' . $test1 . '<br>');

我的测试输出如下:

之前:这里?

之后:

Here's a solution:

echo('Before: ' . $test1 . '<br>');

$vars = array_keys(get_defined_vars());
foreach($vars as $var) {
    if($var == 'GLOBALS' || $var == '_POST' || $var == '_GET' || $var == '_COOKIE' || $var == '_FILES' || $var == '_REQUEST' || $var == '_SERVER' || $var == '_ENV')
        continue;
    unset($var);
}
echo('After: ' . $test1 . '<br>');

My test outputs this:

Before: here?

After:

始终不够爱げ你 2024-12-18 05:29:49

没有内置任何内容,但您可以使用 get_defined_vars() 函数获取所有已定义变量的数组,然后使用 foreach 循环取消它们。这样您只需 4 行即可完成所有操作,而且非常灵活。

$list_of_vars =  array_diff(get_defined_vars(), $GLOBALS); // Was just get_defined_vars() before Marc B corrected me in his post.
foreach($list_of_vars as $var){
    unset($var);
}

编辑:正如 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.

$list_of_vars =  array_diff(get_defined_vars(), $GLOBALS); // Was just get_defined_vars() before Marc B corrected me in his post.
foreach($list_of_vars as $var){
    unset($var);
}

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.

青瓷清茶倾城歌 2024-12-18 05:29:49

我同意油石的观点。 get_define_vars() 非常适合“多次取消设置”技巧。
下面的方法取消设置所有变量。 除了在 foreach 循环内的无名数组中找到的之外的所有内容

foreach(get_defined_vars() as $k=>$y){ 
 if( !in_array( $k, 
 array(
  'myRequredVariableName1',
  'myRequredVariableName2',
  '_ENV',
  '_SESSION',
   '_COOKIE',
  'HTTP_SESSION_VARS',
  'HTTP_COOKIE_VARS'
  )))
  { $k=null; unset($k);} 
    unset($y, $k);
} 

// Check for leftovers
header('Content-type:text/plain; charset=utf-8'); 
var_export(get_defined_vars()); 
exit;

这些值实际上是不带“$”的变量名称,其中变量 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.

foreach(get_defined_vars() as $k=>$y){ 
 if( !in_array( $k, 
 array(
  'myRequredVariableName1',
  'myRequredVariableName2',
  '_ENV',
  '_SESSION',
   '_COOKIE',
  'HTTP_SESSION_VARS',
  'HTTP_COOKIE_VARS'
  )))
  { $k=null; unset($k);} 
    unset($y, $k);
} 

// Check for leftovers
header('Content-type:text/plain; charset=utf-8'); 
var_export(get_defined_vars()); 
exit;

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.

风筝有风,海豚有海 2024-12-18 05:29:49

需要这个,而这里现有的答案并没有真正的帮助。以下内容很简短,切中要点,并且有效:

foreach(get_defined_vars() as $k => $v)
    unset($k);
unset($k, $v);

如果在此之后调用 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:

foreach(get_defined_vars() as $k => $v)
    unset($k);
unset($k, $v);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文