Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:43862 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16126 invoked from network); 6 May 2009 15:16:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 May 2009 15:16:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=ralph@smashlabs.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=ralph@smashlabs.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain smashlabs.com from 67.15.58.61 cause and error) X-PHP-List-Original-Sender: ralph@smashlabs.com X-Host-Fingerprint: 67.15.58.61 openrce.org Linux 2.6 Received: from [67.15.58.61] ([67.15.58.61:42624] helo=users.smashlabs.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9B/16-01656-7C9A10A4 for ; Wed, 06 May 2009 11:16:24 -0400 Received: (qmail 21797 invoked from network); 6 May 2009 10:16:21 -0500 Received-SPF: none (no valid SPF record) Received: from cpe-24-28-26-65.austin.res.rr.com (HELO ralph-macbook.local) (24.28.26.65) by smashlabs.com with (DHE-RSA-AES256-SHA encrypted) SMTP; 6 May 2009 10:16:20 -0500 Message-ID: <4A01A9C3.5010108@smashlabs.com> Date: Wed, 06 May 2009 10:16:19 -0500 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.9) Gecko/20061207 Thunderbird/1.5.0.9 Mnenhy/0.7.4.666 MIME-Version: 1.0 To: internals Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: RecursiveFilterItrator possible issues with inheritance From: ralph@smashlabs.com (Ralph Schindler) Hi all, I'd like to confirm something as a bug and/or design issue. 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). Ideally, this code would work: $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) { $r = new ReflectionClass($this); $this->_otherArg = $otherArg; parent::__construct($iterator); } public function accept() { echo ($this->_otherArg) ? 'Arg Present' : 'Arg NOT Present'; echo PHP_EOL; return true; } The current output would be: ~/tmp/test-rii-filter$ php test-rii-problem.php 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 The ideal output would be: 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 The only proposed change I could would be to make the getChildren() inside of RecursiveFilterIterator use clone & add a method called setIterator() instead of Reflection::newInstance($iterator) Is this possible? Or is extending and overriding getChildren() the best strategy here? Reason I post is b/c it seems like using clone/setIterator() would facilitate better inheritance / better polymorphism. And since the implementation is in the extension, the typical developer would generally not understand how getChildren() really works under the hood. Thanks, Ralph