Constructor and destructor in PHP

Constructor in PHP

Constructor is special type of method in oops because its name is similar to class name.Constructor automatically calls when object will be initializing.If a class name and function name will be similar in that case function is known as constructor.
constructor can be define in 2 category

  • Pre-define Constructor
  • User Define Constructor

Pre-define Constructor is a constructor which can be call by using function __construct().

By using this function we can define a constructor.It is known as predefined constructor. Its better than user defined constructor because if we change class name then user defined constructor treated as normal method.

Note: if predefined constructor and user defined constructor, both define in the same class, then predefined constructor treat like a Constructor while user defined constructor treated as normal method.

class A

{

function testA()

{

echo "This is test method of class A";

}

function A()

{

echo "This is user defined constructor of class A"."
";

}

}

$obj= new A();

$obj->testA();

Output
This is user defined constructor of class A
This is test method of class A

Note : Here we have called testA() method but we didn’t call A() method because it automatically called when object is initialized.

 User Define Constructor is a constructor which can be call by using function className()
lets understand by an example:
 

php

class Simple

{

function test()

{

echo "This is test method of class Simple";

}

function Simple()

{

echo "This is user defined constructor of class Simple"."";

}

}

$obj= new Simple();

$obj->test();

?

Output
This is user defined constructor of class Simple.

This is test method of class Simple

Note : Here we have called test() method but we didn’t call A() method because it automatically called when object is initialized.

Initialize class property using constructor(Value entered by users)

<?php

Class demo
{

Public $first;

Public $second;

function __construct($a,$b)

{

$this->first=$a;

$this->second=$b;

}

function add()

{

$add=$this->first+$this->second;

echo "sum of given no=".$add;

}

}

$obj= new demo($_POST['x'],$_POST['y']);

$obj->add();

?>

<form method="post">

enter your first number<input type="text" name="x"/><br/>

enter your second number<input type="text" name="y"/><br/>

<input type="submit" value="+"/>

</form>

Parametrized constructor

< ?php

class employee {

Public $name;

Public $profile;

function __construct($n,$p) { $this->name=$n;

$this->profile=$p;

echo "Welcome ";

}

function show()

{

echo $this->name."... ";

echo "Your profile is ".$this->profile."
";

}

}

$obj= new employee("sourabh","Manager");

$obj->show();

$obj1= new employee("barkhane","Clerk ");

$obj1->show();

?>

Output
Welcome sourabh… Your profile is Manager
Welcome barkhane… Your profile is Clerk

Destructor in PHP

The Destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

Destructor automatically call at last.

Note : _ _destruct() is used to define destructor.

php

class test

{

function __construct()

{

echo "object is initializing their properties"."</br>";

}

function work()

{

echo "Now works is going "."
";

}

function __destruct()

{

echo "after completion the work, object destroyed automatically";

}

}

$obj= new test();

$obj->work();

//to check object is destroyed or not
echo is_object($obj);

Output
object is initializing their properties
Now works is going
1
after completion the work, object destroyed automatically

Leave a Reply