ADVERTISEMENT
ADVERTISEMENT

PHP find output programs (Class & Objects) | set 3

Find the output of PHP programs | Class & Objects | Set 3: Enhance the knowledge of PHP Class & Objects by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 28, 2021

Question 1:

<?php
    class Sample
    {
        protected function fun()
        {
            echo "Fun() called<br>";
        }
    }

    $obj = new Sample();
    $obj->fun();
?>

Output:

PHP Fatal error:  Uncaught Error: Call to protected method Sample::fun() 
from context '' in /home/main.php:11
Stack trace:
#0 {main}
  thrown in /home/main.php on line 11

Explanation:

The above code will generate syntax error because we cannot access protected member outside the class.

Question 2:

<?php
    class Sample
    {
        public function fun()
        {
            echo "Fun() called<br>";
        }
    }
    
    (new Sample())->fun();
?>

Output:

Fun() called

Explanation:

In the above program, we created a class Sample that contains a public method fun().

The above program, will call method fun(), because (new Sample()) will return the object. Herem we created anonymous object to call method fun().

Question 3:

<?php
    class Sample
    {
        static public function fun()
        {
            echo "Fun() called<br>";
        }
    }
    
    Sample::fun();
?>

Output:

Fun() called

Explanation:

In the above program, we created a class Sample that contains a static method fun(), And we called function fun() using scope resolution operator (::), that will print "Fun() called" on the webpage.

Question 4:

<?php
    class Sample
    {
        public function fun()
        {
            echo "Fun() called<br>";
        }
    }

    S;

    S->fun();
?>

Output:

PHP Parse error:  syntax error, unexpected '->' (T_OBJECT_OPERATOR) 
in /home/main.php on line 12

Explanation:

The above program will generate syntax error, we cannot create an object like, this is the C++ style for object creation. But it is not supported in PHP.

Question 5:

<?php
    class Sample
    {
        public function fun()
        {
            echo "Fun() called<br>";
        }
    }
    $S = new Sample();
    
    if ($S instanceof Sample) 
        echo "Hello";
    else 
        echo "Hiii";
?>

Output:

Hello

Explanation:

In the above program, we created a class Sample that contains method fun(), then we created the object of class Sample. Here, we used the instanceof keyword, which is used to check an object belongs to class or not. Here, object $S belongs to the class Sample, then "Hello" will be printed on the webpage.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.