Home »
PHP »
PHP find output programs
PHP find output programs (Constructors and Destructors) | set 2
Find the output of PHP programs | Constructors & Destructors | Set 2: Enhance the knowledge of PHP Constructors & Destructors by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 28, 2021
Question 1:
<?php
class Sample
{
private $A;
private $B;
public function Sample()
{
$this->A = 10;
$this->B = 20;
}
public function Sample($A, $B)
{
$this->A = $A;
$this->B = $B;
}
public function print ()
{
echo "A: $this->A<br>";
echo "B: $this->B<br>";
}
}
$OB1 = new Sample();
$OB1->print();
$OB2 = new Sample(30, 40);
$OB2->print();
?>
Output:
PHP Fatal error: Cannot redeclare Sample::Sample()
in /home/main.php on line 12
Explanation:
The above program will generate a fatal error because we cannot perform constructor overloading using PHP.
Question 2:
<?php
class Sample
{
private $A;
private $B;
public function Sample():
int
{
$this->A = 10;
$this->B = 20;
return 0;
}
public function print ()
{
echo "A: $this->A<br>";
echo "B: $this->B<br>";
}
}
$OB1 = new Sample();
$OB1->print();
?>
Output:
PHP Fatal error: Constructor Sample::Sample() cannot
declare a return type in /home/main.php on line 2
Explanation:
The above program will generate syntax error because the constructor does not support return type in PHP.
Question 3:
<?php
class Sample
{
public function Sample()
{
print ("Constructor Called<br>");
}
public function ~Sample()
{
print ("Destructor Called<br>");
}
}
$OB = new Sample();
?>
Output:
PHP Parse error: syntax error, unexpected '~'
in /home/main.php on line 8
Explanation:
The above program will generate syntax error, we cannot call destructor using tilde '~' symbol. To implement destructor in a class, we need to use the __destruct() magic function.
Question 4:
<?php
class Sample
{
public function Sample()
{
print ("Constructor Called<br>");
}
public function __DESTRUCT()
{
print ("Destructor Called<br>");
}
}
$OB = new Sample();
?>
Output:
Constructor Called
Destructor Called
Explanation:
In the above program, we created a class Sample that contains constructor and destructor, And we created an object of Sample class then constructor and destructor both will be called and print messages on the webpage.
A constructor is called when an object gets created while the destructor is called when the scope of the object gets finished.
Question 5:
<?php
class Sample
{
public $VAL;
public function __construct()
{
$this->VAL = 10;
}
public function Sample($V)
{
$this->VAL = $V;
}
public function print ()
{
print ("$this->VAL<br>");
}
}
$OB1 = new Sample();
$OB2 = new Sample(20);
$OB1->print();
$OB2->print();
?>
Output:
10
10
Explanation:
In the above program, we defined two constructors, default constructor using __construct() magic function and another is parameterized constructor. Then we created two objects $OB1 and $OB2, both will call default constructor __construct. If we defined more than one constructor in a class, but constructor defined using __construct() will always call, other constructors can be defined but cannot be called.