Bug #17799 Updated: Using $this->$variable_name inside object clears all variable values

From: Date: Mon, 17 Jun 2002 18:35:53 +0000
Subject: Bug #17799 Updated: Using $this->$variable_name inside object clears all variable values
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-10912@lists.php.net to get a copy of this message
ID: 17799 Updated by: sander@php.net Reported By: qdot@numberporn.com -Status: Bogus +Status: Open Bug Type: Class/Object related Operating System: Any PHP Version: CVS New Comment: rodif_bl, this is NOT a bogus. You're right that $this->$undefined_var is not correct, but it should not empty _all_ variables/properties of that object. Previous Comments: ------------------------------------------------------------------------ [2002-06-17 14:09:14] rodif_bl@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is what it should do... if you look at your assignments $this->$a = "blah"; a is a variable not a member.... $this->a = "blah" is the correct syntax for what you want basically what you are doing with $this->$a = "blah"; is assigning the member '' with the text "blah"... cause $a is evaluated to ''. you can use the $this->$a syntax but $a needs to be defined $a = "somemember"; $this->$a = "sometext"; ------------------------------------------------------------------------ [2002-06-17 13:43:27] qdot@numberporn.com Yes, this script produces the same result when run: Output: >php test.php X-Powered-By: PHP/4.2.1 Content-type: text/html a='0' b='0' a='500' b='500' ------------------------------------------------------------------------ [2002-06-17 12:28:33] mfischer@php.net Shorter example: <? class foo { var $a; var $b; function foo() { $this->$notdefined = 0; } function getA() { return "a='" . $this->$a . "'"; } function getB() { return "b='" . $this->$b . "'"; } function setA($a_ = "") { $this->$dontexistseither = $a_; } } $o = new foo(); echo $o->getA() . "\n"; echo $o->getB() . "\n"; $o->setA(500); echo $o->getA() . "\n"; echo $o->getB() . "\n"; ?> Outputs: $ php bug_17799.php a='0' b='0' a='500' b='500' ------------------------------------------------------------------------ [2002-06-17 10:29:29] sander@php.net Does my simple testscript give the same problem? Your test script is too long to serve as a test script, you'll lose track of it at once. Please track the problem down to only a couple of lines. ------------------------------------------------------------------------ [2002-06-17 10:05:45] qdot@numberporn.com Here's the exact script we used when we found this bug: ---- TableProperties.class <? /*********************************************** * This class represents the attributes which define * the properties of a Table. */ class TableProperties { //the attributes which define a table var $widthTable; var $heightTable; var $cellPadding; var $cellSpacing; var $border; var $widthCell; var $heightCell; var $hAlignCell; var $vAlignCell; /** * Constructor of the class. Main purpose is to * initialize the variables with a value as a default. */ function TableProperties() { $this->$widthTable = ""; $this->$heightTable = ""; $this->$cellPadding = 0; $this->$cellSpacing = 0; $this->$border = 0; $this->$widthCell = ""; $this->$heightCell = ""; $this->$hAlignCell = "center"; $this->$vAlignCell = "top"; } /** * Returns the attributes as a key-value pair * that is compatible with HTML. */ function getWidthTablePair() { return ("width='" . $this->$widthTable . "'"); } function getHeightTablePair() { return ("height='" . $this->$heightTable . "'"); } function getCellPaddingPair() { return ("cellpadding='" . $this->$cellPadding . "'"); } function getCellSpacingPair() { return ("cellspacing='" . $this->$cellSpacing . "'"); } function getBorderPair() { return ("border='" . $this->$border . "'"); } function getWidthCellPair() { return ("width='" . $this->$widthCell . "'"); } function getHeightCellPair() { return ("height='" . $this->$heightCell . "'"); } function getHAlignCellPair() { return ("align='" . $this->$hAlignCell . "'"); } function getVAlignCellPair() { return ("valign='" . $this->$vAlignCell . "'"); } /** * Returns the values of the attributes. */ function getWidthTable() { return $this->$widthTable; } function getHeightTable() { return $this->$heightTable; } function getCellPadding() { return $this->$cellPadding; } function getCellSpacing() { return $this->$cellSpacing; } function getBorder() { return $this->$border; } function getWidthCell() { return $this->$widthCell; } function getHeightCell() { return $this->$heightCell; } function getHAlignCell() { return $this->$hAlignCell; } function getVAlignCell() { return $this->$vAlignCell; } /** * Sets the variables to the given value. */ function setWidthTable($widthTable_ = "") { $this->$widthTable = $widthTable_; } function setHeightTable($heightTable_ = "") { $this->$heightTable = $heightTable_; } function setCellPadding($cellPadding_ = "") { $this->$cellPadding = $cellPadding_; } function setCellSpacing($cellSpacing_ = "") { $this->$cellSpacing = $cellSpacing_; } function setBorder($border_ = "") { $this->$border = $border_; } function setWidthCell($widthCell_ = "") { $this->$widthCell = $widthCell_; } function setHeightCell($heightCell_ = "") { $this->$heightCell = $heightCell_; } function setHAlignCell($hAlignCell_ = "") { $this->$hAlignCell = $hAlignCell_; } function setVAlignCell($vAlignCell_ = "") { $this->$vAlignCell = $vAlignCell_; } } ?> ---- test-00.php <?php require("TableProperties.class"); $tp = new TableProperties(); echo $tp->getHeightTablePair() . "<br>"; echo $tp->getWidthTablePair() . "<br>"; echo $tp->getBorderPair() . "<br>"; echo $tp->getVAlignCellPair() . "<br>"; $tp->setHeightTable(500); $tp->setWidthTable("600"); $tp->setBorder(5); //$tp->setVAlignCell("bottom"); echo $tp->getHeightTablePair() . "<br>"; echo $tp->getWidthTablePair() . "<br>"; echo $tp->getBorderPair() . "<br>"; echo $tp->getVAlignCellPair() . "<br>"; ?> ---- Output is: >php test-00.php X-Powered-By: PHP/4.2.1 Content-type: text/html height='top'<br>width='top'<br>border='top'<br>valign='top'<br>height='5'<br>width='5'<br>border='5'<br>valign='5'<br> ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/17799 -- Edit this bug report at http://bugs.php.net/?id=17799&edit=1

« previous php.bugs (#10912) next »