PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » HTML » HTML_QuickForm » Bug #80

HTML_QuickForm::addElement($element) dosen't pass by reference

Details

Submitted2003-10-08 20:08 UTC
Frommarba126 at student dot liu dot se
StatusWont fix
PackageHTML_QuickForm
PHP VersionIrrelevant
OSWindows 98 SE
Roadmaps(Not assigned)

Comments

[2003-10-08 20:08 UTC] 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.

[2003-10-09 09:59 UTC] marba126 at student dot liu dot se

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();
?>

[2003-10-09 10:14 UTC] mansion at php dot net

Wrong. Doing that will throw a call time pass by reference warning. You should always develop your code with error_reporting(E_ALL); Alexey's suggestion is the way to go.