Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在析构函数中取消设置对象属性是否更有效?_Php_Performance_Destructor - Fatal编程技术网

Php 在析构函数中取消设置对象属性是否更有效?

Php 在析构函数中取消设置对象属性是否更有效?,php,performance,destructor,Php,Performance,Destructor,假设对象的属性包含大量数据,在析构函数中取消设置属性是否更有效?还是让php释放该对象分配的内存 class A { protected $foo; protected $bar; protected $baz; public function __construct() { $this->foo = big_amount_of_data(); $this->bar = reference_to_a_big_o

假设对象的属性包含大量数据,在析构函数中取消设置属性是否更有效?还是让php释放该对象分配的内存

class A {
    protected $foo;
    protected $bar;
    protected $baz;

    public function __construct()
    {
        $this->foo = big_amount_of_data();
        $this->bar = reference_to_a_big_object();
        $this->foo = data_from_big_file();
    }

    public function doProcess()
    {
        // do something
    }

    public function __destruct()
    {
        // Should I do this?
        unset(
            $this->foo,
            $this->bar,
            $this->baz
        );
    }
}

简单地说:不,你真的不应该这么做

当您销毁对象及其变量时,最好让PHP对其执行它想要的操作,如果这一切都是为了取消变量以获取一些内存

1) 您永远无法确定使用
unset
取消设置变量会直接返回已使用的内存。它可能是即时的(尽管如此,不是真的,这取决于垃圾收集器何时决定执行其工作),也可能是几分钟后,或者在脚本的最后,多亏了垃圾收集器。这不是一个确定的方法来恢复记忆,仅此而已


2) 变量的可访问性不会有任何变化

赋值null比不赋值好。速度更快,如果有其他变量引用您正在取消设置的变量,则分配null实际上会释放内存,而取消设置不会-其他变量仍将具有数据且不为null值

是否有一些示例代码来显示您的意思?显示您当前的工作代码@ClémentMalet,我更新我的question@VIVEK-MDU,实际上这就是我正在考虑的,我没有一个有效的代码,但是我用一个示例代码更新了我的问题