Jump to content

Recommended Posts

This is a bit of a more advanced question so I am hoping there are some advanced programmers online at this time :). Anyways basically I want to be able to "queue" functions from a class in a single call and am wondering if I am doing it correctly or if there is a better way to do it. Here is my code:

 

<?php
class test {
     private static $one;
     private static $two;
     private static $three;

     public function callOne($val){
          $one .= $val;
          return $this;
     }
     
     public function callTwo($val){
          $two .= $val;
          return $this;
     }

     public function callThree($val){
           $three .= $val;
           return $this;
     }

     public function print(){
          echo $this->one.' '.$this->two.' '.$this->three;
     }
}
?>

 

now when I want to call this I can do:

$test = new test();
$test->callOne('one')->callTwo('two')->callThree('three');
$test->callOne('another one');
$test->print();

 

Is there a better way to do this or is there a different method of doing this? Just wanna make sure I am doing it right lol.

Thanks so basically I am doing it right. Kinda just discovered that on my own because I didnt know what to google but now I do "method chaining". Is this efficient? I assume so because $this should be passed as a reference correct?

We had actually could of have helped you better if you instead of Test had put up a real example of your code. I would create a Façade (or a Builder if you are actually building something in steps) if those methods needs to be called in sequence.

 

Because you are familiar with those functions and how they are called, you can't assume someone else will if they join your project.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.