php - are these functions good to reduce server load? [closed]
good evening,
i have thesefive functions
that i use to reduce server load
:
// unset all vars
function unset_all_vars()
{
$vars = func_get_args();
foreach($vars[0] as $key => $val)
{
unset($GLOBALS[$key]);
}
return serialize($vars[0]);
}
unset_all_vars(get_defined_vars());
// unset all const
function unset_all_const()
{
$vars = func_get_args();
foreach($vars[0] as $key => $val)
{
unset($key);
}
return serialize($vars[0]);
}
unset_all_const(get_defined_constants());
// unset all functions
function unset_all_functions()
{
$vars = func_get_args();
foreach($vars[0] as $key => $val)
{
开发者_开发问答 unset($key);
}
return serialize($vars[0]);
}
unset_all_functions(get_defined_functions());
// unset all classes
function unset_all_classes()
{
$vars = func_get_args();
foreach($vars[0] as $x => $v)
{
unset($x);
}
return serialize($vars[0]);
}
unset_all_classes(get_declared_classes());
// unset all interfaces
function unset_all_interfaces()
{
$vars = func_get_args();
foreach($vars[0] as $x => $v)
{
unset($x);
}
return serialize($vars[0]);
}
unset_all_interfaces(get_declared_interfaces());
?>
function 1 unset all vars
function 2 unset all const function 3 unset all functions function 4 unset all classes function 5 unset all interfacesare they good ?
is there some other functionsbetter than
them ? or additional to
them ?Why use functions like this at all? Even if they would work, a PHP script runs only for a second at most, after which it will clear up everything it has allocated. Freeing things inbetween is particularly useful for scripts that run for a long time and allocate lots of resources that are soon not needed anymore, but this is quite uncommon for web pages, for which PHP is mainly used.
The naming is wrong too, since they don't deallocate 'all' items, but only the ones they are passed.
You cannot unset
(or more correctly, undefine
) a constant, function, class or interface. Nor should you ever need to - it certainly wouldn't "reduce server load", even if you could. And I can't see unsetting all the variables in the global scope making any real positive difference - if you create a large variable and want to free up the resources it has used after use then do it explicitly at the time, unsetting all global variables is highly unlikely to do any good, and it will probably cause harm.
Further to this, all your functions apart from the first one have no effect whatsoever. In a foreach, the variables that are created during the loop are copies of the original data, and unsetting the copy does not affect the original.
PHP is garbage collected so you shouldn't ever need to worry about things like this - read this and this.
精彩评论