Q:

PHP find output programs (Constructors and Destructors) | set 3

0

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();
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to 
function Sample::__construct(), 0 passed in /home/
main.php on line 20 and exactly 1 expected in /home/main.php:10
Stack trace:
#0 /home/main.php(20): Sample->__construct()
#1 {main}
  thrown in /home/main.php on line 10

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:

10
20
30

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:

Constructor called
Constructor called
Constructor called
10
20
30
Destructor called
Destructor called
Destructor called

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:

ID: 101, Name:ROHIT, Salary:1000
ID: 102, Name:SHIKHAR, Salary:800
ID: 103, Name:VIRAT, Salary:1500

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now