Re: addElementGroup loops external loop in an object

From: Date: Tue, 23 Jul 2002 08:52:14 +0000
Subject: Re: addElementGroup loops external loop in an object
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-1959@lists.php.net to get a copy of this message
le 22/07/02 23:14, Daniel Bauke à bonkey@bonkey.pl.eu.org a écrit : > can you help me with that code? > > it unexpectedly loops. when i use same code without > class features it works ok. i have no idea > what's wrong.. > > i'm using PHP Version 4.2.1, Windows NT 5.1 build 2600 > (May 12 2002 23:51:56) on Apache 1.3.26 and QuickForm 2.3 > > please answer me on e-mail, too :-) > > --------------- > require_once 'HTML/QuickForm.php'; > > class testFrmXX { > var $_buttons = array("ok"=>"one", "ok2"=>"two", > "ok3"=>"three"); > var $_form; > > function testFrmXX() { > $this->_form =& new HTML_QuickForm('editForm','POST'); > > $menu = array(); > while (list($btnName, $caption) = each($this->_buttons)) { > print "<hr>"; flush(); // DEBUG > $menu[$btnName] =& > HTML_QuickForm::createElement('text', "$caption" ); > } > $this->_form->addElementGroup($menu, '', 'queryForm', > '&nbsp;'); > print $this->_form->toHtml(); > } > } > > print "..."; > $foo = new testFrmXX(); I am posting this to the list because it's a bit beyond my understanding. After running some tests with your script, firstly, it appears that you are right, there is definitely a loop problem in this special case, and secondly, there are ways to fix this even if I don't really get the logic behind it. One way is to use foreach ($this->_buttons as $btnName => $caption) instead of while (list($btnName, $caption) = each($this->_buttons)). What foreach does, it creates a copy of the array it uses ($this->_buttons in our case). So if you do, $buttons = $this->_buttons before issuing the while loop, it will work fine too, but it will take more memory as with the foreach. In term of performances, foreach is supposed to be a bit faster than while(list, each). This doesn't tell me why I get this endless loop when walking through the instance array. I wonder if it comes from QuickForm ? Bertrand Mansion Mamasam

« previous php.pear.general (#1959) next »