PHP find output programs (static variables, classes, methods) | set 1

Find the output of PHP programs | static variables, classes, methods | Set 1: Enhance the knowledge of PHP static variables, classes, methods concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 25, 2021

Question 1:

<?php
    class Sample
    {
        public static function fun1()
        {
            echo "function fun1() called<br>";
        }
        public function fun2()
        {
            fun1();
            echo "function fun2() called<br>";
        }
    }
    
    $obj = new Sample();
    $obj->fun2();
?>

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function fun1() 
in /home/main.php:10
Stack trace:
#0 /home/main.php(16): Sample->fun2()
#1 {main}
  thrown in /home/main.php on line 10

Explanation:

The above program will generate syntax error, because if you want to call a static function into a non-static function of the same class, then we need to use the self keyword with '::' operator. The correct program is given below:

<?php
    class Sample
    {
        public static function fun1()
        {
            echo "function fun1() called<br>";
        }
        public function fun2()
        {
            self::fun1();
            echo "function fun2() called<br>";
        }
    }
    
    $obj = new Sample();
    $obj->fun2();
?>

Question 2:

<?php
    class Sample
    {
        public static function fun1()
        {
            echo "function fun1() called<br>";
        }
        public function fun2()
        {
            echo "function fun2() called<br>";
        }
    }
    $obj = new Sample();
    $obj::fun1();
    $obj->fun2();
?>

Output:

function fun1() called
function fun2() called

Explanation:

In the above program, we created a class that contains a static function fun1() and a non-static function fun2(). Then we created the object of class Sample, and here we called static function fun1() using scope resolution operator :: and non-static using -> operator. Then the above output will be printed on the webpage.

Question 3:

<?php
    class Sample
    {
        public static function fun1()
        {
            echo "function fun1() called<br>";
        }
        public function fun2()
        {
            echo "function fun2() called<br>";
        }
    }
    
    $obj = new Sample();
    $obj::fun1();
    $obj::fun2();
?>

Output:

function fun1() called
function fun2() called

Explanation:

In the above program, we created a class that contains a static function fun1() and a non-static function fun2(). Then we created the object of class Sample, and here we called both functions fun1() and fun2() using scope resolution operator "::" with an object instead of "->". We can also access non-static members using scope resolution operator "::' in PHP. Then the above output will be printed on the webpage.

Question 4:

<?php
    class Sample
    {
        private static $count = 0;
        public function __construct()
        {
            $count = $count + 1;
        }
    
        public static function GetCount():
            int
            {
                return $count;
            }
        }
        
        $obj1 = new Sample();
        $obj2 = new Sample();
        $obj3 = new Sample();
    
        echo Sample::GetCount();
?>

Output:

PHP Fatal error:  Uncaught TypeError: Return value of 
Sample::GetCount() must be of the type integer, null returned in
 /home/main.php:13
Stack trace:
#0 /home/main.php(21): Sample::GetCount()
#1 {main}
  thrown in /home/main.php on line 13

Explanation:

The above program will generate PHP notices and errors, because if we want to access static data member inside the class then we need to use self keyword, but we did not use in the constructor while using $count.

Question 5:

<?php
class Sample
{
    private static $count = 0;
    public function __construct()
    {
        self::$count = self::$count + 1;
    }

    public static GetCount():
        int
        {
            return self::$count;
        }
    }
    $obj1 = new Sample();
    $obj2 = new Sample();
    $obj3 = new Sample();

    echo Sample::GetCount();
?>

Output:

PHP Parse error:  syntax error, unexpected 'GetCount' (T_STRING), 
expecting function (T_FUNCTION) or const (T_CONST) i
n /home/main.php on line 10

Explanation:

The above program will generate an error because we missed the function keyword in the definition of GetCount() function.





Comments and Discussions!

Load comments ↻





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