foreach with methods returning arrays ??? any ideas ?
| From: | Ipa | Date: | Sat, 17 Aug 2002 15:15:25 +0000 |
| Subject: | foreach with methods returning arrays ??? any ideas ? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-113080@lists.php.net to get a copy of this message | ||
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 ?