diff options
-rw-r--r-- | it_dbi.class | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/it_dbi.class b/it_dbi.class index 153fdd6..356930e 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -143,7 +143,8 @@ function createclass($p) if (substr($classname, 0, 4) != 'PMA_') # It is designed behaviour that an error is generated if this class already exists! { - $code = "class $classname extends it_dbi + $interface = function_exists("interface_exists") && interface_exists("Iterator", false) ? "implements Iterator" : ""; + $code = "class $classname extends it_dbi $interface { function $classname(/* $query ... */) { @@ -677,6 +678,42 @@ function _get_field_info() return $result; } +# +# Implement PHP 5 Iterator interface to make foreach work +# Example: foreach (new T_User('firstname' => "foo") as $foouser) { ... } +# +function current() +{ + return $this; +} + +function key() +{ + return $this->_key; +} + +function next() +{ + $this->iterate(); +} + +function rewind() +{ + if (!$this->_result) # Object without query used in foreach + $this->select(); + + # Only rewind if not already at start and results present + if (!$this->_nofetch && mysql_num_rows($this->_result)) + mysql_data_seek($this->_result, 0); + + $this->iterate(); +} + +function valid() +{ + return (bool)$this->_data; +} + } /* End class it_dbi */ ?> |