final class MyClass {
public $get = array();
public $post = array();
public $cookie = array();
public $files = array();
public $server = array();

   public function __construct() {
$_GET = $this->clean($_GET);
$_POST = $this->clean($_POST);
$_COOKIE = $this->clean($_COOKIE);
$_FILES = $this->clean($_FILES);
$_SERVER = $this->clean($_SERVER);

$this->get = $_GET;
$this->post = $_POST;
$this->cookie = $_COOKIE;
$this->files = $_FILES;
$this->server = $_SERVER;
}

   public function clean($data) {
     if (is_array($data)) {
   foreach ($data as $key => $value) {
unset($data[$key]);

     $data[$this->clean($key)] = $this->clean($value);
   }
} else { 
   $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
} return $data;
}
}
用echo回显了相关变量, new 一个 MyClass 后, 又回显一次, 没什么不同啊..就是奇怪,这些网上的代码,经常性了unset一下一些全局变量.

解决方案 »

  1.   


    不是很明白.比如$_SERVER里面的,unset之后,再回显,还是跟之前一样的内容啊~
      

  2.   

    unset()基本上就是释放的变量内存,或者下面程序中会用同名变量在函数中,unset只能销毁局部变量,并不能销毁全局变量,特别是global引用的变量,可以尝试$GLOBALS数组对于一些有引用的变量,可以这样得到真正的释放
    $foo = null;
    unset($foo);
      

  3.   

    我有个自编程序(大概循环处理本地某目录下5000+的html文档),最初php.ini配置内存limit=8M,报溢出,一直加到128M,终于可以运行完毕然后加入几个unset,再把limit从128M减到16M,都能顺利完成(8M还是不行)……
      

  4.   

    看看手册关于变量的使用范围,写得很明白,其中就有unset的例程