blacklion Posted January 28, 2010 Share Posted January 28, 2010 Hi All, I am having some trouble understanding a bit of code which extends MySQLi. I am trying to move into using MySQLi and at the same time into a more OO approach so forgive me if this is trivial! The code is thus (courtesy of the php.net user contributed notes) class Database_MySQLi extends MySQLi { public function query($query) { $this->real_query($query); return new Database_MySQLi_Result($this); } } class Database_MySQLi_Result extends MySQLi_Result { public function fetch() { return $this->fetch_assoc(); } public function fetchAll() { $rows = array(); while($row = $this->fetch()) { $rows[] = $row; } return $rows; } } I assume this is used as follows (as in another very similar example I have); $db = new Database_MySQLi(.....); $users = $db->query("SELECT * FROM USERS"); foreach ($users as $user) { echo $user->firstname; echo $user->lastname; } I understand what is happening, what I don't understand is how does it know what function ( fetch() / fetch_all() / fetch_object() [as in example] ) to use from the Database_MySQLi_Result class? Thanks for any tips! BL Link to comment https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/ Share on other sites More sharing options...
Mchl Posted January 28, 2010 Share Posted January 28, 2010 It doesn't. Code should look more like this: $db = new Database_MySQLi(.....); $users = $db->query("SELECT * FROM USERS"); while($user = $users->fetch()) { echo $user->firstname; echo $user->lastname; } or $db = new Database_MySQLi(.....); $result= $db->query("SELECT * FROM USERS"); $users = $result->fetchAll(); foreach ($users as $user) { echo $user->firstname; echo $user->lastname; } Link to comment https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003068 Share on other sites More sharing options...
blacklion Posted January 28, 2010 Author Share Posted January 28, 2010 Thanks Mchl. Your explanation makes perfect sense. However, (I should have explained to start with) my actual query is with a MySQLi_ResultWrapper class which uses the iterator and countable interfaces. It seems to work without being explicitly told to retrieve any one function or another - that I can see! The class in question is written by Alejandro Gervasio over at DevShed : http://www.devshed.com/c/a/PHP/Improving-MySQL-Connection-with-Static-Variables-in-PHP-5-Classes/1/ BL Link to comment https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003075 Share on other sites More sharing options...
Mchl Posted January 28, 2010 Share Posted January 28, 2010 What the...? I can't find constructor for that class? Anyway: public function current() is the one responsible for fetching values through foreach() Link to comment https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003079 Share on other sites More sharing options...
blacklion Posted January 28, 2010 Author Share Posted January 28, 2010 Thanks Mchl. I haven't come across Iterators yet really but it makes sense now! About your comment, it is a must to have a constructor in this class? This class seems quite tidy and a good place to start playing with mysqli interface but maybe not...? BL Link to comment https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003140 Share on other sites More sharing options...
Mchl Posted January 28, 2010 Share Posted January 28, 2010 Each class have a default constructor that does nothing. Well... not really... It calls constructor from parent class if it's present. I forgot about it, and hence my confusion. (Because 'user-defined' constructors do NOT call parent constructors). http://www.php.net/manual/en/language.oop5.decon.php Link to comment https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003161 Share on other sites More sharing options...
blacklion Posted January 28, 2010 Author Share Posted January 28, 2010 Thanks Mchl, I understand Link to comment https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003273 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.