function - Continous methods php -
this question has answer here:
- php method chaining? 7 answers
i see in codeigniter sintaxis method->other_method->other_method_again. example:
$this->db->select()->join()->findall();
when try
class myclass{ public function bla(){ echo "bla"; } public function other(){ echo "other"; } } $message = new myclass(); $message->bla()->other();
return:
fatal error: call member function other()
as can codeigniter?
this called method chaining (popularised jquery chaining) , achieved if
return $this
from each method
for example:
class myclass{ public function bla(){ echo "bla"; return $this; // enable method chaining } public function other(){ echo "other"; return $this; // enable method chaining } }
the reason works same following works:
$instance->method1(); $instance->method2();
here each method called on $instance
but if each method returns actual $instance
same $this
, 1 can combine statements ("chain" them):
$instance->method1()/* returns $instance , can used again*/->method2();
that's there it.
Comments
Post a Comment