[PHP] foreach with methods returning arrays ??? any ideas ?

2002-08-17 Thread Ipa

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 nbsp;nbsp; . $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 nbsp;nbsp; . $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




Re: [PHP] foreach with methods returning arrays ??? any ideas ?

2002-08-17 Thread Rasmus Lerdorf

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 nbsp;nbsp; . $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 nbsp;nbsp; . $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




Re: [PHP] foreach with methods returning arrays ??? any ideas ?

2002-08-17 Thread Ipa

yes there is difference in creation returning array.. but i talk about other
thing
IMHO
1. return returns COPY of array in both cases .. no difference is it
method or function
2. here we don't return reference to the same array.
3. foreach - as i have read in most sources Makes a copy of given array.  so
it is 2-nd time i have to get copy of that array so 2-nd foreach dont have
to affect 1-st ...

if the cursor pointing to the same array how can it transparently point
through the method ?

isn't it ?? please correct me if i wrong..


 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 nbsp;nbsp; . $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 nbsp;nbsp; . $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