Can I write chainable functions in CodeIgniter?
So if I have functions like these :
function generate_error(){
return $data['result'] = array('code'=> '0',
'message'=> 'error brother');
}
function display_error(){
$a= '<pre>';
$a.= print_r($data);
$a.= '</pre>';
return $a;
}
I want to call those by chaining them :
echo $this->generate_error()->display_error();
The reason why I want to seperate these functions are because display_error() is only useful for development, so when it comes to production, I can just remove the display_error() or something like that.
Thanks!
To write chainable functions they musy be part of a class, from the function you then return a reference to the current class (usually
$this).If you return anything other than a reference to a class it will fail.
It is also possible to return a reference to another class (e.g. when you use the code igniter active records class
get()function it returns a reference to theDBresultclass)