Alex
Alex

Reputation: 68440

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?

Upvotes: 3

Views: 6955

Answers (8)

Svish
Svish

Reputation: 158021

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.

Upvotes: 0

Spooky
Spooky

Reputation: 1316

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.

Upvotes: 1

Jeff Lambert
Jeff Lambert

Reputation: 24661

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:

Upvotes: 2

Whetstone
Whetstone

Reputation: 1199

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.

Upvotes: 1

Marc B
Marc B

Reputation: 360632

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.

Upvotes: 7

Seralize
Seralize

Reputation: 1127

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.

Upvotes: 2

phihag
phihag

Reputation: 287825

You can wrap the require in an anonymous function:

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

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

Upvotes: 8

user142162
user142162

Reputation:

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');
}

Upvotes: 4

Related Questions