From:             ralph at smashlabs dot com
Operating system: *nix, osx, win32
PHP version:      5.2.9
PHP Bug Type:     SPL related
Bug description:  RecursiveIteratorIterator does not persist state parent in 
getChildren()

Description:
------------
Currently, its difficult to extend RecursiveFilterIterator() with
additional __construct() arguments (for configuring its behavior).  The
reason its difficult is b/c when getChildren() is called, a new instance of
the RFI is created (only way you'd know this is if you look into the source
of RFI).

Workaround currently is this (But is not generic enough to all use
cases):

replace getChildren():

    function getChildren()
    {
        if (empty($this->ref)) {
            $this->ref = new ReflectionClass($this);
        }
        return
$this->ref->newInstance($this->getInnerIterator()->getChildren(),
$this->_otherArgs);
    }

Reproduce code:
---------------
$rArray = array('a' => array('ab', 'ac', 'ad' => array('ada', 'adc')), 'b'
=> array('bb', 'bc'));

$ri = new RecursiveArrayIterator($rArray);
$filter = new MyRFI($ri, 'argument');
$iterator = new RecursiveIteratorIterator($filter);

foreach ($iterator as $key => $item) {
    echo $key . ' - ' . $item . PHP_EOL;
}

class MyRFI extends RecursiveFilterIterator
{
    protected $_otherArg = null;

    public function __construct($iterator, $otherArg = null)
    {
        $this->_otherArg = $otherArg;
        parent::__construct($iterator);
    }

    public function accept()
    {
        echo ($this->_otherArg) ? 'Arg Present' : 'Arg NOT Present';
        echo PHP_EOL;
        return true;
    }
}

Expected result:
----------------
Arg Present
Arg Present
0 - ab
Arg Present
1 - ac
Arg Present
Arg Present
0 - ada
Arg Present
1 - adc
Arg Present
Arg Present
0 - bb
Arg Present
1 - bc 

Actual result:
--------------
Arg Present
Arg NOT Present
0 - ab
Arg NOT Present
1 - ac
Arg NOT Present
Arg NOT Present
0 - ada
Arg NOT Present
1 - adc
Arg Present
Arg NOT Present
0 - bb
Arg NOT Present
1 - bc


-- 
Edit bug report at http://bugs.php.net/?id=48166&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=48166&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=48166&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=48166&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=48166&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=48166&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=48166&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=48166&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=48166&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=48166&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=48166&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=48166&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=48166&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=48166&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=48166&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=48166&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=48166&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=48166&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=48166&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=48166&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=48166&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=48166&r=mysqlcfg

Reply via email to