Is the __destruct method useful?

8

I read about the __destruct method on these two links:

And I have not yet found the utility of the __destruct method, even more than @ user166390, (the user who answered the question of the 2nd link) said:

  

"A destructor has nothing to do with releasing memory directly ..."

According to him, I would have another purpose that I did not quite understand. Could anyone give me a practical explanation of using __destructor ? Is an example where __destructor is required?

    
asked by anonymous 23.12.2015 / 20:28

1 answer

7

It has, but first you have to understand at what times it runs.

The concept is similar to that of other languages such as C ++ , __destruct is always fired when the reference of a "calling class" is "destroyed", for example:

$test = new MinhaClasse();
$test = null; //A partir daqui o __destruct provavelmente será chamado (ou no fim do script)

If there is nothing like this in the script and the variable is accessible by other scopes, then the object will stay there until the shutdown of the script occurs, then the __destruct will be triggered only when the script is finished.

When to use __destruct

Imagine that you have a series of variables in the class or vector variables, large and when the class is destroyed you want to clean them, so you can use __destruct , this helps in releasing a bit of memory of how GC behaves, can not control).

class Foo {
    private $bar = array();

    public function __construct() {
         $this->bar = range(1, 1000000);//Gera um grande vetor
    }

    public function __destruct() {
         $this->bar = null;//libera variável
    }
}

You can also use this to disconnect from socket, or from mysql without having to call mysqli_close , for example:

class Conexao {
    private $link = null;
    public function __construct() { //Auto-conecta
         $this->link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    }

    public function __destruct() {
         mysqli_close($this->link);//desconecta
         $this->link = null;
    }
}

When calling this:

$test = new Conexao();

//...Aqui fica todo o processo que vai usar a conexão

$test = NULL; //A partir daqui provavelmente a classe será destruída

//...Outras tarefas

In this way you will not have to disconnect, __destruct will take care of this for you.

About the comment:

  

"A destructor has nothing to do with releasing memory directly ..."

He said "immediately," that is to say yes, "help" to release, but this does not happen immediately, it is used to release "references" or other tasks, such as disconnecting from socket . The memory release will depend on the GC as I mentioned earlier.

__destruct does not automatically release references, it is a method you should write yourself, in this method you will have to tell how and what should be released from the "internal references" of the class, then the references being free GC will take care of trying to free memory.

    
23.12.2015 / 20:47