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

Asked

Viewed 337 times

3

A big difference from PHP to Java, which is my native language, is that in Java there is no way to destroy objects that are in disuse, because the JVM already does it for us automatically, with the Garbage Collector. In PHP, there is no Garbage Collector, and yes destructive methods, so the question that remains is:

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

2 answers

6

Java has no destructor but has finishers and other mechanisms more modern and considered better. 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 something that does something before it is destroyed.

So if you understand the concept of Java resource completion/closure you know how __destruct PHP. If you don’t understand, I hope you’ll only do simple things.

PHP has GC yes, just isn’t the same as Java. Have you ever needed to free up memory? It’s a problem if you don’t create destructor in most cases?

To tell the truth in 99% of the cases where PHP is used, neither GC nor destructor is very necessary. In fact there is a lot of class that I see around that strictly speaking should have a constructor and it doesn’t have, even so it doesn’t cause problems because PHP is a language of script.

Since PHP’s GC is deterministic it works the same as 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 so-called try-resource that finalizes the object so it is no longer necessary. And if it’s used as a member of an object, it practically forces you to have a finalizer in the object that contains it. In the destruction by the GC the finalizer is only called if he has not been called before.

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

You should use a destructor whenever you have some feature that needs a finish, IE, you need to close a connection, a file, etc. But as I said, no one does it and in PHP it gives in the same in almost all cases.

Destructor runs whenever object is no longer used by code.

The closing or reloading of a page does not interfere with this, since the page is in the client and PHP is in the server, are completely separate things, using another technology. The end of script that generates the page or does something else will surely call the destructor if nothing catastrophic doesn’t happen before.

If the script is called again every memory environment of the previous no longer exists and everything will be created again. And that’s why the destructor doesn’t make much difference in PHP, although conceptually it should always exist.

  • Nice explanation Maniero, very explanatory. Let me ask another question on that same hook. The _destruct() methods of the classes being used in a certain Script are executed when this Script is finished with die() or, in the case of CLI, when the command is aborted?

  • 1

    When the script is terminated normally yes, but if it is closed off it is not called. The die() terminates normally, only can not guarantee the order that the methods will be executed.

3

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

I think you’re wrong. PHP: Garbage Collection

Where and when to use destructor method?

The concept of destruct equals the language C++:

The destructor method will be called as soon as there are no other References to a particular Object, or in any order During the shutdown.

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

You can use the method __destruct when there is a need to perform some operation (automatically) only when the object will no longer be used. For example, release some Resource or release 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 method __destruct, since there are no more references to the object, the connection to the database will be closed.

The link below can give you some more idea of how Garbage Collector works and the PHP references:

https://stackoverflow.com/questions/40348781/object-not-being-destroyed-until-end-of-the-script-if-it-registers-spl-autoload/41462603#41462603

Destructor method runs automatically when page is closed?

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

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, which has an application running on a server, PHP does not have an application that is always running. The PHP application is created when a request is received. The application executes all your script (as requested) and then after all the execution and output, the application is destroyed. Therefore, the method __destruct will always be executed.

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

According to the explanation about the application, your objects will always be destroyed and new ones will be created. This is a feature of the PHP architecture.

http://php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor

Browser other questions tagged

You are not signed in. Login or sign up in order to post.