PHP Tutorial

PHP Examples

PHP Practice

PHP program to implement a cascaded function calls

Here, we are going to learn how to implement a cascaded function call in PHP?
Submitted by Nidhi, on November 11, 2020 [Last updated : March 13, 2023]

Cascaded Function Calls in PHP

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

PHP code to implement a cascaded function calls

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.

PHP Class & Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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