ADVERTISEMENT
ADVERTISEMENT

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

Find the output of PHP programs | Class & Objects | Set 2: 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
{
    public $val;

    function set($v)
    {
        $this->val = $v;
    }

    function ADD($B):
        Sample
        {
            $temp = new Sample();
            $temp->val = $this->val + $B->val;
            return $temp;
        }

        function print ()
        {
            echo $this->val;
        }
    }

    $obj1 = new Sample();
    $obj2 = new Sample();

    $obj1->set(100);
    $obj2->set(200);

    $obj3 = $obj1->ADD($obj2);

    $obj3->print();
?>

Output:

Result: 300

Explanation:

In the above program, we created a class Sample that contains data member $val. Here, we also created three methods set(), ADD(), and print().

The set() method is used to set the value of data member $val, and the ADD() method is used to add the value of the specified object with the value of the current object and return the temporary object that contains the addition of both.

Then we created two objects $obj1 and $obj2 initialized with 100 and 200.

$obj3 = $obj1->ADD($obj2);

In the above statement, $obj3 assigned by the object returned by method ADD() then print the value using the print() method.

Question 2:

<?php
    class Sample
    {
        public $val;
    
        function set($v)
        {
            $this->val = $v;
        }
    
        function print ()
        {
            echo $this->val . '<br>';
        }
    }
    
    $obj[0] = new Sample();
    $obj[1] = new Sample();
    
    $obj[0]->set(10);
    $obj[1]->set(20);
    
    $obj[0]->print();
    $obj[1]->print();
?>

Output:

Result: 300

Explanation:

In the above program, we created a class Sample that contains data member $val. Here, we also created three methods set() and print().

Here, we created an array of objects and initialized with 10 and 20 and print values of using an index with an object.

Question 3:

<?php
class Sample
{
    public $val;

    function set($v)
    {
        $this->val = $v;
    }

    function ADD($B):
        Sample
        {
            $temp = new Sample();
            $temp->val = $this->val + $B->val;
            return $temp;
        }

        function print ()
        {
            echo $this->val;
        }
    }

    $obj1 = new Sample();
    $obj2 = new Sample();

    $obj1->set(100);
    $obj2->set(200);

    $obj3 = $obj1->ADD($obj2);
 * ($obj3) . print ();
?>

Output:

PHP Parse error:  syntax error, unexpected '*', expecting end of file 
in /home/main.php on line 32 

Explanation:

The above program will generate syntax error, because of the below statement.

*($obj3).print();

In the above statement, we tried to access print() using the pointer, but pointers are not supported in PHP.

Question 4:

<?php
class Sample
{
    function fun1()
    {
        echo "Fun1() called<br>";
    }
    function fun2()
    {
        echo "Fun2() called<br>";
    }
    function fun3():
        Sample
        {
            echo "Fun3() called<br>";
        }

    }
    $obj = new Sample();
    $obj->fun1()
        ->fun2()
        ->fun3();
?>

Output:

Fun1() called
PHP Fatal error:  Uncaught Error: Call to a member function fun2() on null 
in /home/main.php:21
Stack trace:
#0 {main}
  thrown in /home/main.php on line 21

Explanation:

In the above program, we are trying to implement a cascaded function call, but here object $obj will call fun1(), other function will not call. To implement a cascaded function call, then we need to use $this.

Question 5:

<?php
class Sample
{
    function fun1():
        Sample
        {
            echo "Fun1() called<br>";
            return $this;
        }
        function fun2():
            Sample
            {
                echo "Fun2() called<br>";
                return $this;
            }
            function fun3():
                Sample
                {
                    echo "Fun3() called<br>";
                    return $this;
                }

            }
            $obj = new Sample();
            $obj->fun1()
                ->fun2()
                ->fun3();
?>

Output:

Fun1() called
Fun2() called
Fun3() called

Explanation:

In the above program, we created a class Sample that contains three methods fun1(), fun2(), and fun3(). Each method returns the current object using $this. Then finally we can implement a cascaded function call using $this.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

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.