Find the output of PHP programs | Constructors & Destructors | Set 3: Enhance the knowledge of PHP Constructors & Destructors by solving and finding the output of some PHP programs.
Question 1:
<?php
class Sample
{
public $VAL;
public function Sample()
{
$this->VAL = 10;
}
public function __construct($A)
{
$this->VAL = $A;
}
public function print ()
{
print ("$this->VAL<br>");
}
}
$OB1 = new Sample();
$OB2 = new Sample(20);
$OB1->print();
$OB2->print();
?>
Question 2:
<?php
class Sample
{
public $VAL;
public function __construct($A)
{
$this->VAL = $A;
}
public function print ()
{
print ("$this->VAL<br>");
}
}
$OBJ_ARR = array(
new Sample(10) ,
new Sample(20) ,
new Sample(30)
);
$OBJ_ARR[0]->print();
$OBJ_ARR[1]->print();
$OBJ_ARR[2]->print();
?>
Question 3:
<?php
class Sample
{
public $VAL;
public function __construct($A)
{
$this->VAL = $A;
echo "Constructor called<br>";
}
public function __destruct()
{
echo "Destructor called<br>";
}
public function print ()
{
print ("$this->VAL<br>");
}
}
$OBJ_ARR = array(
new Sample(10) ,
new Sample(20) ,
new Sample(30)
);
$OBJ_ARR[0]->print();
$OBJ_ARR[1]->print();
$OBJ_ARR[2]->print();
?>
Question 4:
<?php
class Employee
{
public $ID;
public $NAME;
public $SALARY;
public function __construct($I, $N, $S)
{
$this->ID = $I;
$this->NAME = $N;
$this->SALARY = $S;
}
public function printEmpDetail()
{
printf("<BR>ID: %d, Name:%s, Salary:%s", $this->ID, $this->NAME, $this->SALARY);
}
}
$OBJ1 = new Employee(101, "ROHIT", 1000);
$OBJ2 = new Employee(102, "SHIKHAR", 800);
$OBJ3 = new Employee(103, "VIRAT", 1500);
$OBJ1->printEmpDetail();
$OBJ2->printEmpDetail();
$OBJ3->printEmpDetail();
?>
Answer 1:
Output:
Explanation:
The above program will generate syntax error because we created constructor using __construct() with an argument, but we created object $OB1 with no argument that's why it will generate a syntax error. Because 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.
Answer 2:
Output:
Explanation:
In the above program, we created a class Sample that contains data member $VAL, and a constructor and a method print().
The constructor will initialize the data member and print() method will print the value of data member $VAL. Then we created an array of objects and initialized with different values and print them.
Answer 3:
Output:
Explanation:
In the above program, we created a class Sample that contains data member $VAL, constructor, destructor, and a method print().
The constructor will initialize the data member and print() method will print the value of data member $VAL, and destructor called when the scope of the object gets finished. Then we created an array of objects and initialized with different values and print them.
Answer 4:
Output:
Explanation:
In the above program, we created a class Employee that contains data members $ID, $NAME, $SALARY, and we created constructor and printEmpDetail() function to print the employee details.
Then we created three objects with different values and print them using printEmpDetail() on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer