Q:

PHP program to implement a cascaded function call

belongs to collection: PHP Classes & Objects Programs

0

Here, we will implement a cascaded function call using $this, it means we can call multiple functions in a single code statement.

All Answers

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

Program/Source Code:

The source code to implement cascaded function call is given below. The given program is compiled and executed successfully.

<?php
//PHP program to implement cascaded function call
class Sample
{
    public function fun1()
    {
        print ("Fun1() called<br>");
        return $this;
    }

    public function fun2()
    {
        print ("Fun2() called<br>");
        return $this;
    }

    public function fun3()
    {
        print ("Fun3() called<br>");
        return $this;
    }
}

$S = new Sample();

$S->fun1()
    ->fun2()
    ->fun3();
?>

Output:

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

Explanation:

In the above program, we created a class Sample that contains three functions fun1()fun2(), and fun3(). Here, all functions return the $this. That's why we are able to call multiple functions in a single statement.

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

total answers (1)

PHP Classes & Objects Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP program to create a constant using define() fu... >>
<< PHP program to convert hours, minutes, and seconds...