আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-4: Constructor Method and Destructor Methods

What is the Constructor Method in PHP?

constructor Method in PHP

The method that automatically executes the first time when creating a new object from class, it’s called constructor method in PHP. Constructor Method is predefined Public Method. To declare any constructor, we have to write two times the underscore (_) before the construct word after the public function. That’s mean we have to declare any constructor like this: public function __construct(). Let’s see an Example:

<?php
class oop{
 public function __construct(){
     echo "Welcome to PHP World!";
 }
 public function sayHello(){
     echo "Hello World";
 }
  
 
}
$obj=new oop; /* Now Automatically Execute __construct Method
                  And Will show "Welcome to PHP World " First.*/
 
echo "<br>";
 
$obj->sayHello(); // Result: Hello World
 
?>

Result

Welcome to PHP World
Hello World

How to Send Argument to the Parameter of Constructor Method?

If there is a Parameter in a Constructor Method of a Class, Then if you want to send any arguments to that constructor Method, the arguments must be sent with parenthesis to the class object. See the example below:

class Car {
  private $model;
   
  //__construct
  public function __construct ($model)
  {
    $this -> model = $model;
  }
    
  public function getCarModel()
  {
    return ' The car model is: ' . $this -> model;
  }
}
   
//We pass the value of the variable once we create the object
$car1 = new Car("Mercedes"); // Passing Argument to Constructor Parameter
   
echo $car1 -> getCarModel();

Result:

The car model is: Mercedes.

What is the Destructor Method in PHP?

Destructor Method in PHP

When the object has finished its job, then the method in the class which is automatically executed or execute at the end, that method is called Destructor Method in PHP. Destructor Method is predefined Public Method. To declare any destructor, we have to write two times the underscore (_) before the destruct word after the public function. That’s mean we have to declare any destructor like this: public function __destruct(). Let’s see an Example:

<?php
class oop{
 public function __construct(){
     echo "Welcome to PHP World!";
 }
 public function sayHello(){
     echo "Hello World";
 }
  
 public function __destruct(){
     echo "<br>I'm about to disappear - bye bye!";
 }
 
}
$obj=new oop;
 
echo "<br>";
 
$obj->sayHello();
 
?>

Result:

Welcome to PHP World!
Hello World
I'm about to disappear - bye bye!

Can the Destructor Method in PHP receive an argument?

No it can’t. Actually it doesn’t need to. However, you can receive arguments with any other method and can put that argument to any property and you can return that property via Constructor Method. See the example below:

<?php
class oop{
private $name;
 public function __construct($val){
     return $this->name=$val;
 }
 public function sayHello(){
     echo "Hello World";
 }
  
 public function __destruct(){
     echo "<br>Bye bye $this->name!";
 }
 
}
$obj=new oop("Imran");
 
echo "<br>";
 
$obj->sayHello();
 
?>

Result

Hello World
Bye Bye Imran!

Why Will You Use Destructor Method?

Destructor Method is used to finish anything. Works like file closing, database connection, and session destruction are done by Destructor Method.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

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