#80 [WFx]: HTML_QuickForm::addElement($element) dosen't pass by reference

From: Date: Thu, 09 Oct 2003 09:59:58 +0000
Subject: #80 [WFx]: HTML_QuickForm::addElement($element) dosen't pass by reference
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-22518@lists.php.net to get a copy of this message
ID: 80 User updated by: marba126 at student dot liu dot se Reported By: marba126 at student dot liu dot se Status: Wont fix Bug Type: HTML_QuickForm Operating System: Windows 98 SE PHP Version: Irrelevant New Comment: I just got a brigth idea (that seems to work). However it's a workaround rather than a fix. It's a much better to call addElement(&$element) - note '&'! This might be worth a note in the manual because the "fix" presented by avb@php.net isn't always applicable. (In a future version of PHP maybe this can be fixed with method overloading?) <?php require_once ("HTML/QuickForm.php"); $form = new HTML_QuickForm(); $text =& $form->createElement('text', 'testText', 'This should be "Set after addElement() call" :', 'size=40'); $text->setValue("Set before addElement() call"); $form->addElement(&$text); $text->setValue("Set after addElement() call"); $form->display(); ?> Previous Comments: ------------------------------------------------------------------------ [2003-10-09 03:51:19] avb@php.net There is no easy fix for this, as addElement() has to also accept the element type (usually a constant) as its first parameter and thus it cannot be passed by reference. The easiest workaround would be to use just addElement() instead of createElement()/addElement(): <?php require_once ("HTML/QuickForm.php"); $form = new HTML_QuickForm(); $text =& $form->addElement('text', 'testText', 'This should be "Set after addElement() call" :', 'size=40'); $text->setValue("Set after addElement() call"); $form->display(); ?> ------------------------------------------------------------------------ [2003-10-08 16:08:41] marba126 at student dot liu dot se Description: ------------ Unexpected beaviour: using $element->setValue(), after $element has been added to the form with addElement($element), will have no effect. This is probably because addElement() dosen't pass by reference. Thus addElement() creates a copy of the element you added (which will not be affected when you change the origin element). HTML_QuickForm ver. 3.1.1 Reproduce code: --------------- <?php require_once ("HTML/QuickForm.php"); $form = new HTML_QuickForm(); $text =& $form->createElement('text', 'testText', 'This should be "Set after addElement() call" :', 'size=40'); $text->setValue("Set before addElement() call"); $form->addElement($text); $text->setValue("Set after addElement() call"); $form->display(); ?> Expected result: ---------------- The value of the text input field is expected to be "Set after addElement() call" when form is displayd. Actual result: -------------- The value of the text input field is "Set before addElement() call" when form is displayd. ------------------------------------------------------------------------ -- Edit this bug report at http://pear.php.net/bugs/bug.php?id=80&edit=1

« previous php.pear.dev (#22518) next »