PHP program to implement a cascaded function call

Here, we are going to learn how to implement a cascaded function call in PHP?
Submitted by Nidhi, on November 11, 2020

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

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.