Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1094569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:59:24+00:00 2026-05-16T23:59:24+00:00

My question is rather simple, given: class MyClass{ function a(){ echo F.A ; }

  • 0

My question is rather simple, given:

class MyClass{
   function a(){
       echo "F.A ";
   }
   function b(){
       echo "F.B ";
   }
}

$c=new MyClass;
$c->a()->b()->b()->a();

So that it will output:

F.A F.B F.B F.A

What changes to code need to be made for this to work, or should it work as is or even just what this is called. If I could get whatever this term is called I could research it mysqlf, but I am not quite sure what to Google.

Thanks in Advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:59:25+00:00Added an answer on May 16, 2026 at 11:59 pm

    Stringing together methods like that is called “chaining.”

    return $this; in each method will enable chainability, since it keeps passing the instance from one method to the other, maintaining the chain.

    You have to explicitly do this, since PHP functions will return NULL by default.

    So, you just need 2 more lines.

    <?php
        class MyClass{
       function a(){
           echo "F.A ";
           return $this; // <== Allows chainability
       }
       function b(){
           echo "F.B ";
           return $this;
       }
    }
    
    $c=new MyClass;
    $c->a()->b()->b()->a();
    ?>
    

    Live Example

    Take a look at this article by John Squibb for a further exploration of chainability in PHP.


    You can do all sorts of stuff with chainability. Methods commonly involve arguments. Here’s an “argument chain”:

    <?php
       class MyClass{
       private $args = array();
       public function a(){
           $this->args = array_merge($this->args, func_get_args());
           return $this;
       }
       public function b(){
           $this->args = array_merge($this->args, func_get_args());
           return $this;
       }
       public function c(){
           $this->args = array_merge($this->args, func_get_args());
           echo "<pre>";
           print_r($this->args);
           echo "</pre>";       
           return $this;
       }   
    }
    
    $c=new MyClass;
    $c->a("a")->b("b","c")->b(4, "cat")->a("dog", 5)->c("end")->b("no")->c("ok");
    
    // Output:
    //   Array ( [0] => a [1] => b [2] => c [3] => 4 [4] => cat 
    //           [5] => dog [6] => 5 [7] => end )
    //   Array ( [0] => a [1] => b [2] => c [3] => 4 [4] => cat 
    //           [5] => dog [6] => 5 [7] => end [8] => no [9] => ok )
    ?>
    

    Live Example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this rather simple question about Scala. Given that i have to following
my question is rather simple, if you have an pure virtual class (interface) but
I have a rather simple question that I could normally debug myself, but I
I'm rather new to WPF, so maybe this is a simple question. I have
This is a rather simple question, I have a base class which implements common
Simple question, is it possible to give an alias to the output function in
My question is rather simple. Let's suppose I'm executing the algorithm A star (search
Have a rather simple question. Does anyone knows if i can use jparallax both
I think its a rather simple question but I couldn't really find an answer
Rather a simple question. But the implications are vast. Over the last few weeks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.