Anyone running 5.3.3 can help to confirm this bug?

Just found this annoying PHP bug. Can someone confirm before I create a ticket?

<?php
class Test {

    protected $loaded;
    
    public function set( ) {
        $this->loaded[] = 'core';
    }

    public function getLoaded( ) {
        return $this->loaded;
    }
}

$Test = new Test();
$Test->set( );

while( list( , $var ) = each( $Test->getLoaded( ) ) ) {
	echo 'h';
}

// Basically it produce infinite hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.....
// So be prepared to stop your browser.
?>

Update: Just found similar bug report @ php. Its a default PHP behavior so not consider a bug:


<?php
class Test {

    protected $loaded;
    
    public function set( ) {
        $this->loaded[] = 'core';
    }

    public function getLoaded( ) {
        return $this->loaded;
    }
}

$Test = new Test();
$Test->set( );
$loaded = $Test->getLoaded( ); // while loop is calling "anew" so should do this instead

while( list( , $var ) = each( $loaded ) ) {
    echo 'h';
}
?>

Test::getLoaded() will always return a new copy of the stored array on each iteration of the while loop, hence the infinite loop.

You can confirm this with the following snippet.


class Test
{
  protected
    $storage = array();

  public function __construct(){
    $this->storage = range('a', 'z');
  }

  public function getStorage(){
    return $this->storage;
  }
}

$test = new Test;

while(list( , $char) = each($test->getStorage())){
  echo $char; # aaaaaaaaaaaaaaaaaaaaaa... ad infinitum
}

Notice how we never hit ‘b’ ?

Alternatively, you could you foreach instead.


class Test
{
  protected
    $storage = array();

  public function __construct(){
    $this->storage = range('a', 'z');
  }

  public function getStorage(){
    return $this->storage;
  }
}

$test = new Test;

foreach($test->getStorage() as $char){
  echo $char;
}