#45747 [Opn->Bgs]: Request magic method __iteration()

From: Date: Thu, 07 Aug 2008 23:09:52 +0000
Subject: #45747 [Opn->Bgs]: Request magic method __iteration()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-127748@lists.php.net to get a copy of this message
ID: 45747 Updated by: johannes@php.net Reported By: info at netmosfera dot it -Status: Open +Status: Bogus Bug Type: Feature/Change Request Operating System: Irrelevant PHP Version: 5.3.0alpha1 New Comment: Have a look at the ITerator and IteratorAggregate interfaces for implementing inner and outer iterators in PHP. Previous Comments: ------------------------------------------------------------------------ [2008-08-07 22:51:21] info at netmosfera dot it Description: ------------ Hello, im asking for a magic method in oop __iteration() that is how i want it work a special __get, but for the iteration (foreach) please just read the code :D thank you Reproduce code: --------------- <?php class Test { private $pointer = -1; private $fields = array(); private $values = array(); public function __set($name, $value) { $this->fields[] = $name; $this->values[] = $value; } public function __get($name) { list($key) = array_keys($this->fields, $name); return $this->values[$key]; } public function __iteration($name, $value) { /* my useful [...] code */ $this->pointer++; return array($this->fields[$this->pointer], $this->values[$this->pointer]); } } $php = new Test(); $php->just = "a"; $php->a = "b"; $php->simple = "c"; $php->test = "d"; foreach($php as $field => $value) { echo $field . "=" . $value . "<br>"; } ?> Expected result: ---------------- just = a a = b simple = c test = d Actual result: -------------- no result ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45747&edit=1

« previous php.bugs (#127748) next »