php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27586 ArrayObject::getIterator crashes with [] assignment
Submitted: 2004-03-13 11:35 UTC Modified: 2004-03-13 14:53 UTC
From: ebay at webcrumb dot com Assigned: helly (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.0.0b4 (beta4) OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ebay at webcrumb dot com
New email:
PHP Version: OS:

 

 [2004-03-13 11:35 UTC] ebay at webcrumb dot com
Description:
------------
I'm not sure if I'm using ArrayObject correctly (I thought you could use the primitive type 'array' directly to fetch iterators initially but it seems that's not the case), but I can't tell due to the lack of documentation ...

All latest service packs/patches are installed.



Reproduce code:
---------------
class StudentList implements IteratorAggregate
{
	private $students;
	
	public function __construct() {
		$this->students = new ArrayObject( array() );
	}
	
	public function add( Student $student ) {
		if( !$this->contains( $student ) ) {
                        // XXXX: this causes the crash!
			$this->students[] = $student;
		}
	}

        // ... snip ...

	public function getIterator() {
		return $this->students->getIterator();
	}
}

$students = new StudentList();
$students->add( new Student( '01234123', 'Joe', 'Blo', 'j.blo@here.com' ) );
$students->add( new Student( '00000014', 'Bob', 'Moe', 'b.moe@qut.edu.au' ) );

foreach( $students as $student ) {
	echo $student->getId(); // first parameter passed to the above constructors
}


Expected result:
----------------
This should print out "0123412300000014".

Actual result:
--------------
The Apache 2 service crashes hard.

Resolved by:

public function __construct() {
  $this->students = array();
}

// ... snip ...

public function getIterator() {
  $arr = new ArrayObject( $this->students );
  return $arr->getIterator();
}

Using this code, the expected result is printed out.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-13 14:53 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC