CodeIgniter Forums
question about method chaining - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: question about method chaining (/showthread.php?tid=2999)



question about method chaining - El Forum - 09-04-2007

[eluser]杨帅[/eluser]
i got a "syntax error, unexpected T_OBJECT_OPERATOR" error, and i searched the forum, now i know it's because the php4 not allow method chaining. i shouldn't use the syntax such as
Code:
$this->db->orderby()
in my php4 web server.
instead of the upper one ,i should use
Code:
$db=$this->db;
$db->orderby();
but i searched the source of codeigniter , i found lots of statement such as:
Code:
if ($this->CI->db->cachedir == '')
        return $this->CI->db->cache_off();
    $path = $this->CI->db->cachedir;
    if ($this->CI->db->db_debug)
        return $this->CI->db->display_error('db_invalid_cache_path');
is this method chaining? i donno whecher it can run property in php4 server. anybody know this? great thanks.


question about method chaining - El Forum - 09-04-2007

[eluser]coolfactor[/eluser]
Code:
$this->db->orderby()
That code works fine under PHP4. Were you missing a semi-colon at the end?

Method chaining (PHP5 only) is a very convenient shortcut which lets you access multiple methods on the same object in a single line of code:

Code:
$this->db->where('something')->orderby('something')->limit(5);

where(), orderby(), and limit() are all methods of $this->db (an instance of the Database class).

The CodeIgniter code you posted is not method chaining. It's just accessing a single property or method on objects attached to other objects.


question about method chaining - El Forum - 09-04-2007

[eluser]杨帅[/eluser]
thank you coolfactor, you are really cool!