diff options
author | Christian Schneider | 2008-10-01 14:04:03 +0000 |
---|---|---|
committer | Christian Schneider | 2008-10-01 14:04:03 +0000 |
commit | 13626f1556884806dbcc387b7268975de2c0aac4 (patch) | |
tree | aefb040b8da1be20f06fe30b8dc9c97096f2adff /it_dbi.class | |
parent | 950e1abaabda5a8b86326af95fd6af5216c2c6df (diff) | |
download | itools-13626f1556884806dbcc387b7268975de2c0aac4.tar.gz itools-13626f1556884806dbcc387b7268975de2c0aac4.tar.bz2 itools-13626f1556884806dbcc387b7268975de2c0aac4.zip |
Added PHP5 iterator to it_dbi: Allows foreach (new T_User...)
Diffstat (limited to 'it_dbi.class')
-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 */ ?> |