OOP Destructor: In object-oriented programming, the destructors run after the page HTML is sent. This method is used in many cases to complete variables or simply run scripts that need to run in the final moment of the class called.

In PHP, the destructor method is created from the following syntax:

function _destruct(){

// Code here


You just need to implement the _DESTRUCT () method, just with that name. In the following example you can see in practice the use of a class destructor:


private $name;
class MyClassDestroyable {
function __construct() {
print “In constructorn”;
$this->name = “MyDestructableClass”;

function __destruct() {
print “Destructing ” . $this->name . “n”;

$obj = new MyDestroyableClass();

The above class is initialized by storing in the variable “name” the name of the class. The destructor, in turn, named after the use of the class, displays the information on screen, with the message that the class is being destroyed. In the example, the print command was used, but other controls may be implemented as, for example, methods for closing sessions and variables.

However, the use of destructors needs to be done taking into consideration two important points:

1. Destructors parents will not be called implicitly by the engine. To run a parent destructor, one must make an explicit call to parent :: __ destruct () in the destructor body;

2. Destructors arte called during the end of the script after the HTTP headers are sent. In the current script directory, the closure phase can be different with some SAPIs (e.g. Apache).

Check out more content on our blog!
Learn all about Scriptcase.

By ,

June 12, 2015


You might also like…

How to Create and Read QR Codes and Barcodes

“Point your cell phone camera to read the QR code.” This phrase has been increasingly recurring...

Why is Low-Code essential for any business?

Low-Code is the IT term that refers to the process of using little code to develop software and app...

“The Preventus Medical Center system has more than 222,000 patients and 115 professionals.” (DRey Solutions)

Check out how DRey Solutions uses Scriptcase to streamline and synthesize the process of maintenanc...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.