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

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.
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 print ()
        {
            echo "A: $this->A<br>";
            echo "B: $this->B<br>";
        }
    }
    
    function main()
    {
        $E = new Sample();
        $E->print();
    }
    
    main();
?>

Output:

A: 10
B: 20

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.

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

Output:

PHP Fatal error:  Constructor Sample::Sample() cannot be static 
in /home/main.php on line 2

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.

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

Output:

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

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.

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

Output:

PHP Parse error:  syntax error, unexpected 'Sample' (T_STRING), 
expecting function (T_FUNCTION) or const (T_CONST) in 
/home/main.php on line 7

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.

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

Output:

PHP Fatal error:  Cannot redeclare Sample::__construct() 
in /home/main.php on line 12

Explanation:

The above program will generate the fatal error because we cannot perform constructor overloading using magic function __construct() in PHP.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.