Discussion:
Clone FilterIterator/IteratorIterator?
Peter Oberst
2014-03-24 20:37:26 UTC
Permalink
Hi,

I want to use an Iterator that returns an the inner iterator, with an
additional FilterIterator applied.
The returned iterator should be separated from my inner iterator, to
avoid errors like 'ArrayIterator::next(): Array was modified outside'.
To avoid this, I think it might be the best to simply clone the inner iterator.

Unfortunatly, if the innerIterator is an FilterIterator, so cloning
will throw an exception:
PHP Fatal error: Call to undefined method ArrayIterator::getIterator()

Is there a good reason why i can't clone a FilterIterator?

Testcase:
<?php

class MyIterator extends FilterIterator {
public function accept() {
return true;
}
}

$arr = new ArrayObject(array());
$it1 = new MyIterator($arr->getIterator());
$it2 = $it1->getIterator();
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-03-24 20:48:44 UTC
Permalink
Post by Peter Oberst
Hi,
I want to use an Iterator that returns an the inner iterator, with an
additional FilterIterator applied.
The returned iterator should be separated from my inner iterator, to
avoid errors like 'ArrayIterator::next(): Array was modified outside'.
To avoid this, I think it might be the best to simply clone the inner iterator.
Unfortunatly, if the innerIterator is an FilterIterator, so cloning
PHP Fatal error: Call to undefined method ArrayIterator::getIterator()
Is there a good reason why i can't clone a FilterIterator?
<?php
class MyIterator extends FilterIterator {
public function accept() {
return true;
}
}
$arr = new ArrayObject(array());
$it1 = new MyIterator($arr->getIterator());
$it2 = $it1->getIterator();
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Not sure why you came to that conclusion, but the error you are getting is
because MyIterator ($it1) extends FilterIterator extends IteratorIterator
implements OuterIterator implements OuterIterator OuterIterator extends
Iterator extends Traversable, none which have a getIterator() method
implemented.

Sorry for duplicate email, forgot to reply all.
Peter Oberst
2014-03-24 21:04:15 UTC
Permalink
Oh sorry, it's too late and too mutch fiddeling with the testcase.

Corrected testcase:

<?php

class MyIterator extends FilterIterator {
public function accept() {
return true;
}
}

$arr = new ArrayObject(array());
$it1 = new MyIterator($arr->getIterator());
$it2 = clone $it1;



Throws Fatal error: Trying to clone an uncloneable object of class MyIterator
Post by Aziz Saleh
Post by Peter Oberst
Hi,
I want to use an Iterator that returns an the inner iterator, with an
additional FilterIterator applied.
The returned iterator should be separated from my inner iterator, to
avoid errors like 'ArrayIterator::next(): Array was modified outside'.
To avoid this, I think it might be the best to simply clone the inner iterator.
Unfortunatly, if the innerIterator is an FilterIterator, so cloning
PHP Fatal error: Call to undefined method
ArrayIterator::getIterator()
Is there a good reason why i can't clone a FilterIterator?
<?php
class MyIterator extends FilterIterator {
public function accept() {
return true;
}
}
$arr = new ArrayObject(array());
$it1 = new MyIterator($arr->getIterator());
$it2 = $it1->getIterator();
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Not sure why you came to that conclusion, but the error you are getting is
because MyIterator ($it1) extends FilterIterator extends IteratorIterator
implements OuterIterator implements OuterIterator OuterIterator extends
Iterator extends Traversable, none which have a getIterator() method
implemented.
Sorry for duplicate email, forgot to reply all.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...