Re: php5 iterate an object
| From: | Jochem Maas | Date: | Mon, 23 Jan 2006 10:17:57 +0000 |
| Subject: | Re: php5 iterate an object | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-229063@lists.php.net to get a copy of this message | ||
Henrik Gemal wrote:
I have an object that looks like this: Test Objectthe class Test must implement 'Iterator' e.g. class Test implements Iterator {
/*
* Iterator methods
*/
public function rewind(){}
public function current(){}
public function valid(){}
public function next(){}
public function key(){}
public function count(){}
}
you'll have to fill in the body of those methods
yourself!
once you have done that yuou will be able to do something like:
$t = new Test();
foreach($t as $key => $value) {
echo $key,' - ',$value,"\n";
}
I'd question what you are trying to do; it sounds liek an Iterator is not the right tool for the job.( [config] => TestConfig Object ( [file:protected] => test.conf [kill:protected] => 1 )[location:private] => "test"} I'm trying to write an PHP5 iterator that can iterate over an object but I'm not sure how. The manual for PHP5 iterators seems to only work with arrays. How do I iterate over an object and only get the name of the attribute ("config") and not the entire name ("Test::config")