Constructors and Destructors in PHP

Constructors and destructors play a very vital role when we talk about object-oriented programming.

So, When creating a new object, often it ’ s useful to set up certain aspects of the object at the same time. For example, you might want to set some properties to initial values, fetch some information from a database to populate the object, or register the object in some way. Similarly, when it ’ s time for an object to disappear, it can be useful to tidy up aspects of the object, such as closing any related open files and database connections, or unsetting other related objects. Like most OOP languages, PHP provides you with two special methods to help with these tasks. An object ’ s constructor method is called just after the object is created, and its destructor method is called just before the object is freed from memory.


Creating Constructors in PHP

Normally, when you create a new object based on a class, all that happens is that the object is brought into existence. (Usually you then assign the object to a variable or pass it to a function.) By creating a constructor method in your class, however, you can cause other actions to be triggered when the object is created.

To create a constructor, simply add a method with the special name __construct() to your class.
(That ’ s two underscores, followed by the word “ construct, ” followed by parentheses.) PHP looks for this special method name when the object is created; if it finds it, it calls the method.

Here ’ s a simple example:

<?php
class MyClass {
 function __construct() {
 echo “Welcome to Padhle.com!!”;
 }
}

$obj = new MyClass; //Welcome to Padhle.com!!
?>

The output will be:

Welcome to Padhle.com!!

The class, MyClass, contains a very simple constructor that just displays the message. When the code then creates an object from that class, the constructor is called and the message is displayed.

You can also pass arguments to constructors, just like normal methods. This is great for setting certain properties to initial values at the time the object is created. The following example shows this principle in action:

<?php
class Person {
 private $_firstName;
 private $_lastName;
 private $_age;

 public function __construct( $firstName, $lastName, $age ) {
 $this- > _firstName = $firstName;
 $this- > _lastName = $lastName;
 $this- > _age = $age;
 }

 public function showDetails() {
 echo “$this- > _firstName $this- > _lastName, age $this- > _age < br / > ”;
 }
}

$p = new Person( “Harry”, “Walters”, 28 );
$p- > showDetails(); // Displays “Harry Walters, age 28”
?>

The output will be:

Harry Walters, age 28

The Person class contains three private properties and a constructor that accepts three values,
setting the three properties to those values. It also contains a showDetails() method that displays
the property values. The code creates a new Person object, passing in the initial values for the three
properties. These arguments get passed directly to the __construct() method, which then sets the
property values accordingly. The last line then displays the property values by calling the showDetails() method.


Destructors in PHP

Destructors are useful for tidying up an object before it ’ s removed from memory. For example, if an
object has a few files open, or contains data that should be written to a database, it ’ s a good idea to close
the files or write the data before the object disappears.
You create destructor methods in the same way as constructors, except that you use __destruct()
rather than __construct() :

<?php

class Myclass{
function __destruct() {
// (Clean up here)
}

}

?>

Note that, unlike a constructor, a destructor can ’ t accept arguments.

An object ’ s destructor is called just before the object is deleted. This can happen because all references to it have disappeared (such as when the last variable containing the object is unset or goes out of scope), or when the script exits, either naturally or because of an error of some sort. In each case, the object gets a chance to clean itself up via its destructor before it vanishes.

Leave a Comment

Your email address will not be published. Required fields are marked *