Freeing memory with PHP using __destruct

0

Is it necessary to use the __destruct method and set the variables of the class with null to free up memory more efficiently?

For example:

class Teste {
    public $foo;
    public $bar;
    // ...

    public function __construct() {
        // ...    
    }

    public function __destruct() {
        $this->foo = null;   
        $this->bar = null;   
    }

}
    
asked by anonymous 09.05.2015 / 21:46

2 answers

2

The __destruct is automatically called at the end of the script as it says in the documentation Destrutores são chamados durante o encerramento do script tendo os cabeçalhos HTTP enviados.

And an addendum: assigning null to a variable still consumes memory because there is still a reference to the property that has null value.

    
09.05.2015 / 23:17
5

I do not think so, after all __destruct will be called automatically when all references are released, or when the script ends.

In addition, if you set to null does not free memory, by itself, it only marks as a candidate for garbage collection, which is exactly what _destruct does.

    
09.05.2015 / 21:57