We know that __destruct
is called when the object is destroyed. Logically, what happens if the object is destroyed? Well, it means it’s no longer available. So if it has resources open, it makes sense to close those resources as it’s being destroyed or cleaning up after itself. Also because PHP will close resources on termination for you doesn’t mean that it’s bad to explicitly close them when you no longer need them (or good to not close them).
Some real examples are:
- Closing custom database connector/wrapper connection
- Deletion of temporary folders
- Cleaning up caching
- Spooling the queue of logging messages to db/file