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

Question:

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

Where and when to use the destructor method? Does the destructor method run automatically when the page is closed? If I F5 the page, will the old object be replaced with a new one, or will both stick in memory?

Answer:

Java doesn't have a destructor but it does have finalizers and other mechanisms that are more modern and considered better. Actually the destructor was created more to finish something, so the term is bad. Do not understand this method as something that destroys the object, rather it does something before it is destroyed.

So if you understand Java's resource closing/closing concept, you know what PHP's __destruct . If you don't understand, I hope you just do simple things.

PHP does have GC , it's just not the same as Java. Have you ever needed to free up memory? Is it okay if you don't create destructor in most cases?

As a matter of fact in 99% of cases where PHP is used, neither GC nor destructor is very necessary. There are actually a lot of classes that I see out there that strictly speaking should have a constructor and don't, yet it doesn't cause any problems because PHP is a scripting language.

As PHP's GC is deterministic it works the same as C++, so as soon as the object is no longer needed and has no references to it, the destructor is already called.

In Java another mechanism is needed since object release is not deterministic and may never even be called by the GC. It uses the try-resource which terminates the object as soon as it is no longer needed. And if it's used as a member of an object, it practically forces you to have a finalizer on the object that contains it. In GC destruction the finisher is only called if it has not been called before.

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

You should use a destructor whenever you have some resource that needs termination, ie you need to close a connection, a file, etc. But as I said, nobody does that and in PHP it's 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, 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 doesn't happen first.

If the script is called again, the entire memory environment from the previous one no longer exists and everything will be created again. And that's why the destructor doesn't make much of a difference in PHP, even though conceptually it should always exist.

Scroll to Top