Search code examples
phpfunctionvariablesunset

Unset all defined variables from the current function


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?


Solution

  • 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.