function - OOP PHP Class calling function1()->function2() from one line call -


i in new quandary, , have been trying work it, have gotten nothing errors i'm determined learn how it, , after bit of searching looks cannot done want be.

in class there 2 or more functions(each can called separably[sp]):

public function functionone($some_var){     other code run; } public function functiontwo($some_other_var){     other code run after functionone(), not called here; } 

from caller (model):

$tclass = new testclass(); $result = tclass->functionone()->functiontwo(); echo $result; 

is possible php? know works in frameworks laravel, learned it. nice... , asking if doing wrong? or ist possible, if can more google had very little information on subject.

this called fluent interface , work method doing

return $this; 

most should practice make setters fluent. tend method cannot fluent getters, otherwise, other methods perfect fit fluent interface.

class calculator {     private $result = 0;      public multiplication($number) {         $this->result *= $number;         return $this;     }      public addition() {         $this->result += $number;         return $this;     }      /* ... */      public getresult() {         return $this->result;     } } 

usage:

$calculator = new calculator(); echo $calculator->addition(10)->multiplication(2)->getresult(); // 20 

Comments

Popular posts from this blog

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -