【发布时间】:2017-06-03 03:08:42
【问题描述】:
解除$data有什么目的吗?如果它包含大数据,我应该取消它吗?
<?php
require 'db.php';
class Test{
public $id = 0;
public $name;
public function __construct()
{
$this->getUserInfo();
echo $this->name;
}
private function getUserInfo()
{
global $db;
$query = $db->prepare('SELECT id,name FROM users WHERE group = :g LIMIT 1');
if ($query->execute(array('g' => 'admin')))
{
$data = $query->fetch(); // <--
$this->id = $data[0];
$this->name = $data[1];
return true;
}
}
}
(new Test);
?>
【问题讨论】:
-
现在没有你使用的 php 版本。但有时在某些 php 版本中,局部变量并没有被抓取收集器正确清除,所以如果你想确保这样做
$data=null;unset($data);。 -
这里有很多关于该主题的阅读:stackoverflow.com/questions/2461762/force-freeing-memory-in-php 也阅读评论以获得概述。
-
@JustOnUnderMillions 谢谢,我使用的是 PHP 7.0.9。我认为它会正确清洁它。
-
FasterThanLight PHP 哦,好的。那么,别担心。但可以阅读php中的references和references of references。