Q:

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

belongs to collection: PHP Find Output Programs

0

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.

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

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

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

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

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

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

Answer 2:

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.

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

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

total answers (1)

PHP Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP find output programs (static variables, classe... >>
<< PHP find output programs (define Constant) | set 1...