jwilson122 Posted November 1, 2010 Share Posted November 1, 2010 Hey, I was wondering if anyone could give me an example script or show me how to make a class like such: $do->Query("SELECT * FROM users")->Where("id = 1")->extra("LIMIT 1"); See, I know how to make a class and functions inside of the class to make: $do->Query("SELECT * FROM users"); $do->Where(""id = 1"); $do->extra("LIMIT 1"); But I would rather have the single line link.. $do->Query("SELECT * FROM users")->Where("id = 1")->extra("LIMIT 1"); Is this considered OOP (object oriented programming) ? I've searched and searched for this, tried many things.. nothings working :/ any help? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/ Share on other sites More sharing options...
joel24 Posted November 2, 2010 Share Posted November 2, 2010 for the query function in the class I would have the input to be function Query($query, $where, $extra) { //rarrararara $sql = "$query $where $extra"; //execute and return results } then to call it $do->Query("SELECT * FROM users", "WHERE id=1", "LIMIT 1"); Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/#findComment-1129236 Share on other sites More sharing options...
jwilson122 Posted November 2, 2010 Author Share Posted November 2, 2010 for the query function in the class I would have the input to be function Query($query, $where, $extra) { //rarrararara $sql = "$query $where $extra"; //execute and return results } then to call it $do->Query("SELECT * FROM users", "WHERE id=1", "LIMIT 1"); I didn't even think of doing it that way haha! Not bad idea Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/#findComment-1129238 Share on other sites More sharing options...
trq Posted November 2, 2010 Share Posted November 2, 2010 To use method chaining as in your example you simply need to ensure that each method returns $this. Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/#findComment-1129245 Share on other sites More sharing options...
jwilson122 Posted November 2, 2010 Author Share Posted November 2, 2010 To use method chaining as in your example you simply need to ensure that each method returns $this. when you said method, that means function right? Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/#findComment-1129246 Share on other sites More sharing options...
trq Posted November 2, 2010 Share Posted November 2, 2010 when you said method, that means function right? Yes. Functions within a class are referred to as methods of that class. Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/#findComment-1129248 Share on other sites More sharing options...
jwilson122 Posted November 2, 2010 Author Share Posted November 2, 2010 Would you guys recommend just doing like.. $_db->query("SELECT * FROM bla bla"); ?? Or making a class chain? Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/#findComment-1129262 Share on other sites More sharing options...
trq Posted November 2, 2010 Share Posted November 2, 2010 The chain method is a fair bit more complex within the class itself. Quote Link to comment https://forums.phpfreaks.com/topic/217504-class/#findComment-1129271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.