How does the destructor ("__destruct" method) work in PHP?


A big difference from PHP for Java, which is my native language, is that in Java there is no way to destroy obsolete objects, since JVM already does this for us automatically, with garbage collector . In PHP, there is no garbage collector , but the destructor methods, so the question that remains is:

Where and when to use the destructor method? Is the destructor method executed automatically when the page is closed? If I give F5 on the page, will the old object be replaced with a new one or will both be kept in memory?

asked by anonymous 19.11.2017 / 15:39

2 answers


Java does not have a destructor but has finalizers and other more modern and considered better mechanisms. In fact the destructor was created more to finalize something, so the term is bad. Do not understand this method as something that destroys the object, but rather does something before it is destroyed.

So if you understand the concept of finalizing / closing Java resources you know how __destruct of PHP. If you do not understand, I hope you do just simple things.

PHP has GC yes , just not the same as Java. Have you ever needed to free up memory? Is it a problem if I do not create a destructor in most cases?

To tell the truth in 99% of cases where PHP is used neither GC nor destructor is very necessary. In fact it has a lot of class I see out there that strictly speaking should have a constructor and does not have it anyway it does not cause problems because PHP is a script language. Since the PHP GC is deterministic, it works just like C ++, so once the object is no longer needed and has no references to it, the destructor is already called.

In Java another mechanism is needed since the release of the object is not deterministic and may even never be called by the GC. It uses the try-resource function that finalizes the object so it is no longer needed. And if it is used as a member of an object, it almost obliges to have a finalizer on the object that contains it. In the destruction by GC the finalizer is only called if it was not called before.

Because of the determinism of PHP the destructor is much simpler.

You should use a destructor whenever you have a resource that needs a termination, ie you need to close a connection, a file, and so on. But as I said nobody does this and in PHP it gives the same in almost all cases.

The destructor is executed whenever the object is no longer used by the code.

Closing or reloading a page does not interfere with this since the page is on the client and PHP is on the server, they are completely separate things, using another technology. The end of the script that generates the page or does something else certainly calls the destructor if nothing catastrophic does not happen sooner.

If the script is called again every previous memory environment no longer exists and everything will be created again. And this is the reason that the destructor does not make much difference in PHP, although conceptually it should always exist.

19.11.2017 / 16:09

In PHP, there is no Garbage Collector, but destructor methods

I think you're wrong. PHP: Garbage Collection


Where and when to use the destructor method?

The concept of destruct is equal to the C ++ language:


The destructor method will be called as soon as there are other references to a particular object , or in any order during the shutdown sequence.

You do not directly use the __destruct method, but PHP will use it automatically as soon as there is no further reference to the object.

You can use the __destruct method when there is a need to perform some operation (automatically) only when the object will no longer be used. For example, releasing some resource or releasing objects that are associated with your object.

The most common example is with database connections:

class Connection {

    private $pdo;

    public function __construct() {
         $this->pdo = new PDO(/** connection data **/);

    public function __destruct() {
         //PDO se desconecta automaticamente quando não houver mais referências ao objeto
         //Ou seja, internamente utiliza o método __destruct
         $this->pdo = null;

You can interpret that PDO also uses the __destruct method, because, since there are no more references to the object, the connection to the database will be closed.

The link below may give you some idea of how the garbage collector works and the PHP references:


Does the destructor method run automatically when the page is closed?

The method runs whenever the object no longer has any references or PHP script finishes its execution. That is, when your site finished loading completely, the __destruct method has already been executed by PHP. The methods will be executed even if the script is stopped using the exit / die functions. They will not be executed by any execution failure or if the exit / die function is called within a __destruct method.


The destructor will be called even if script execution is stopped using exit (). Calling exit () in a destructor will prevent the remaining shutdown routines from executing.

Unlike Java and .NET, where there is an application running on a server, PHP does not have an application that is always running. The PHP application is created the moment a request is received. The application executes all its script (according to the request) and then, after all execution and output, the application is destroyed. Therefore, the __destruct method will always be executed.


If I give F5 on the page, will the old object be replaced with a new one or will both be kept in memory?

As explained in the application, your objects will always be destroyed and new objects will be created. This is a peculiarity of the PHP architecture.


19.11.2017 / 16:06