PHP Constructor Types:
1. Default PHP Constructor:
-Default PHP constructor has no parameter or arguments, but the values to the default constructor can be passed dynamically
-We can define constructor by using
function __construct().
Example:
<?php
class Filmyzilla
{
function __construct()
{
echo "I am PHP Default Constructor";
}
}
$ob=new Filmyzilla();
?>
Output: I am PHP Default Constructor2. Parametrized PHP constructor:
-Parametrized PHP constructor takes the parameters and also you can pass different values to the data members.
-The parameterized constructor is a constructor that accepts parameters and is normally used to initialize member variables at the time of object creation.
Example:
<?php
class Filmyzilla
{
function __construct($n1,$n2)
{
$this->n1=$n1;
$this->n2=$n2;
}
function Add()
{
echo "Addition",$this->n1+$this->n2;
}
}
$ob=new Filmyzilla(1,2);
$ob->Add();
?>
Output: Addition 3PHP Destructor:
<?php
class Filmyzilla
{
function __construct($n1,$n2)
{
$this->n1=$n1;
$this->n2=$n2;
}
function __destruct()
{
echo "<br>I am PHP Destructor|| Destroyed";
}
function Add()
{
echo "Addition",$this->n1+$this->n2;
}
}
$ob=new Filmyzilla(1,2);
$ob->Add();
?>
I am PHP Destructor|| Destroyed
MSBTE I Scheme Questions: (4Marks)
Q. Explain PHP Constructor and Destructor.
Q.Describe Default Constructor with suitable Example.
Q.Describe Paramertized Constructor with suitable Example.

Comments
Post a Comment
If you have any query, please let us know