The code is very different. In your x() function you create a new array every time you call x(). In the class example, you create the array once when you instantiate the class and then return that same array each time you call the gety() method. When PHP iterates over arrays, it maintains an array cursor so it knows where in the array it is. Since you keep returning the same array, the array cursor is kept.
-Rasmus On Sat, 17 Aug 2002, Ipa wrote: > Have a strange problem with methods.. > > if i use function to return some array and doing 2-level nested foreach(), > for example > function x(){ > return array(1, 2,3); > } > > foreach(x() as $key){ > echo $key ."<br>"; > foreach(x() as $key2){ > echo " " . $key2 . "<br>"; > } > } > > everything works fine ... the output is > 2 > 2 > 3 > 3 > 2 > 3 > > > But when i do it with object method and property i behaves unwanted, and > breaks loop after first nested foreach() stops > > class Foo{ > var $y; > function Foo(){ > $this -> y = array(2,3); > } > > function gety(){ > return $this -> y; > } > } > > $x = new Foo; > > foreach($x->gety() as $key){ > echo $key ."<br>"; > foreach($x->gety() as $key2){ > echo " " . $key2 . "<br>"; > } > } > > it returns > > 2 > 2 > 3 > > Can anybody explain what is it ? a normal behavior or bug ? > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php