3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ConstructorException extends Exception {} class Foo extends ArrayObject { public function __construct( $arr = array(), $flags = 0, $iterator = 'FooIterator' ) { parent::__construct( $arr, $flags, $iterator ); } /** * @return ArrayIterator */ public function getIterator() { $class = $this->getIteratorClass(); return new $class($this); } } class FooIterator extends ArrayIterator { function __construct( $array = array(), $flags = 0 ) { unset( $array[0] , $array[1] ); parent::__construct( $array, $flags ); } } try { $f = new Foo( array( 1, 2, 3 ) ); $it = $f->getIterator(); $it->offsetSet(5, 10); $f[7] = 11; while ($it->valid()) { echo $it->key() . ' => ' . $it->current() . "\n"; $it->next(); } if ( get_class( $it ) !== 'FooIterator' ) { throw new Exception( 'Expected iterator to be FooIterator.' ); } die( "FAIL\n" ); } catch ( ConstructorException $e ) { die( "PASS\n" ); } catch ( \Exception $e ) { die( sprintf( "ERROR: %s\n", $e->getMessage() ) ); }

preferences:
33.21 ms | 404 KiB | 5 Q