If you’re anything like me, then you want to see the finished version of the previous “MySQLi_ResultWrapper” class, now that it fully implements the Iterator SPL interface. In that case, look at the following example, which shows the class’s source code: class MySQLi_ResultWrapper extends MySQLi_Result implements Iterator { private $_pointer = 0;
// fetch row as an object public function fetchObject() { if (!$row = $this->fetch_object()) { return NULL; } return $row; }
// fetch row as an associative array public function fetchAssocArray() { if (!$row = $this->fetch_assoc()) { return NULL; } return $row; }
// fetch row as an enumerated array public function fetchNumArray() { if (!$row = $this->fetch_row()) { return NULL; } return $row; }
// fetch all rows public function fetchAll($type = MYSQLI_ASSOC) { if ($type !== MYSQLI_ASSOC AND $type !== MYSQLI_NUM AND $type !== MYSQLI_BOTH) { $type = MYSQLI_ASSOC; } if (!$rows = $this->fetch_all($type)) { return NULL; } return $rows; }
// get definition information on fields public function fetchFieldsInfo() { if (!$fieldsInfo = $this->fetch_fields()) { throw new Exception('No information available for table fields.'); } return $fieldsInfo; }
// get definition information on next field public function fetchFieldInfo() { if (!$fieldInfo = $this->fetch_field()) { throw new Exception('No information available for current table field.'); } return $fieldInfo; }
// move pointer in result set to specified offset public function movePointer($offset) { $offset = abs((int)$offset); $limit = $this->num_rows - 1; if ($limit <= 0 OR $offset > $limit) { return NULL; } unset($limit); return $this->data_seek($offset); }
// reset result set pointer (implementation required by 'rewind()' method in Iterator interface) public function rewind() { $this->_pointer = 0; $this->movePointer($this->_pointer); return $this; }
// get current row set in result set (implementation required by 'current()' method in Iterator interface) public function current() { if (!$this->valid()) { throw new Exception('Unable to retrieve current row.'); } $this->movePointer($this->_pointer); return $this->fetchObject(); }
// get current result set pointer (implementation required by 'key()' method in Iterator interface) public function key() { return $this->_pointer; }
// move forward result set pointer (implementation required by 'next()' method in Iterator interface) public function next() { ++$this->_pointer; $this->movePointer($this->_pointer); return $this; }
// determine if result set pointer is valid or not (implementation required by 'valid()' method in Iterator interface) public function valid() { return $this->_pointer < $this->num_rows; } } Mission accomplished. Now the “MySQLi_ResultWrapper” class suits the requirements imposed by Iterator, as it implements all of the interface’s methods. Even though at this point the class is capable of iterating over a retrieved data set by using a “foreach” loop, it does not have a method that allows it to count the number of rows in the set. Fortunately, this issue can be easily addressed by making the class an implementer of the Countable SPL interface. However, that will be addressed in the next tutorial in this series. Final thoughts In this second part of the series, I made the previous "MySQLi_ResultWrapper” class a full implementer of the Iterator SPL interface. As you saw for yourself, implementing the rest of the methods declared by the interface was indeed a straightforward process, and allowed us to traverse MySQL result sets by using a plain “foreach” construct. In the upcoming tutorial, I’m going to give the aforementioned class the ability to count rows in data sets by implementing the “count()” method declared by the Countable SPL interface. Don’t miss the forthcoming article!
blog comments powered by Disqus |
|
|
|
|
|
|
|