Find the output of PHP programs | Constructors & Destructors | Set 1: Enhance the knowledge of PHP Constructors & Destructors by solving and finding the output of some PHP programs.
Question 1:
<?php
class Sample
{
private $A;
private $B;
public function Sample()
{
$this->A = 10;
$this->B = 20;
}
public function print ()
{
echo "A: $this->A<br>";
echo "B: $this->B<br>";
}
}
function main()
{
$E = new Sample();
$E->print();
}
main();
?>
Question 2:
<?php
class Sample
{
private $A;
private $B;
public static function Sample()
{
$this->A = 10;
$this->B = 20;
}
public function print ()
{
echo "A: $this->A<br>";
echo "B: $this->B<br>";
}
}
function main()
{
$E = new Sample();
$E->print();
}
main();
?>
Question 3:
<?php
class Sample
{
private $A;
private $B;
public function Sample($A, $B)
{
$this->A = $A;
$this->B = $B;
}
public function print ()
{
echo "A: $this->A<br>";
echo "B: $this->B<br>";
}
}
function main()
{
$OB1 = new Sample();
$OB2 = new Sample(20, 30);
$OB1->print();
$OB2->print();
}
main();
?>
Question 4:
<?php
class Sample
{
private $A;
private $B;
public Sample()
{
$this->A = 10;
$this->B = 20;
}
public Sample($A, $B)
{
$this->A = $A;
$this->B = $B;
}
public function print ()
{
echo "A: $this->A<br>";
echo "B: $this->B<br>";
}
}
function main()
{
$OB1 = new Sample();
$OB1->print();
$OB2 = new Sample(30, 40);
$OB2->print();
}
main();
?>
Question 5:
<?php
class Sample
{
private $A;
private $B;
public function __construct()
{
$this->A = 10;
$this->B = 20;
}
public function __construct($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();
?>
Answer 1:
Output:
Explanation:
In the above program, we created a class Sample with two data members $A and $B. The Sample class also contains a default constructor and print() member function.
The default constructor is used to initialize data members, and the print() method is used to print the value of data members.
Then we defined a function main(), here we created the object of Sample class and call the print() method that will print values of data members on the web page.
Answer 2:
Output:
Explanation:
The above program will generate syntax error because we created a constructor with a static keyword. But we cannot create a static constructor in PHP.
Answer 3:
Output:
Explanation:
The above program will generate syntax error because we created the object $OB1 with no argument default constructor. But default constructor is not defined in the Sample class. that's why it will generate the error.
Answer 4:
Output:
Explanation:
The above program will generate syntax error because we did not use the function keyword in the definition of constructors of the Sample class.
Answer 5:
Output:
Explanation:
The above program will generate the fatal error because we cannot perform constructor overloading using magic function __construct() in PHP.
need an explanation for this answer? contact us directly to get an explanation for this answer