0

I have a question about how is the best way to realize it.

class name {
  public function method($a){ 
    $this->a = $a; 
  }
  public function two($b){
    $this->b = $b;
  }
}

How I call $class->method('a')->two('b'); ?

return __CLASS__; // self?

on every method? or what? idk what is the best way or how all frameworks realize it.

If anyone can guide me I'll be very greatful... thanks!

0

1 Answer 1

9

It's done by returning $this in every method.

class name {
  public function method($a){ 
    $this->a = $a;
    return $this; 
  }
  public function two($b){
    $this->b = $b;
    return $this; 
  }
}

Anyway there are many opponents (including me) of this convention for a few reasons.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm curious as to what your reasons against this convention are. Personally I've been using this convention since my C++ days.
Search for "method chaining". There's a lot of discussion on the Internet. Here an example: stackoverflow.com/questions/16976150/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.