Bug #17799 Updated: Using $this->$variable_name inside object clears all variable values
| From: | sander@php.net | Date: | Mon, 17 Jun 2002 18:58:35 +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-10920@lists.php.net to get a copy of this message | ||
ID: 17799
Updated by: sander@php.net
Reported By: qdot@numberporn.com
Status: Bogus
Bug Type: Class/Object related
Operating System: Any
PHP Version: CVS
New Comment:
OK, you're right. Objects apparently can have properties named "". Not
a bug -> bogus.
Previous Comments:
------------------------------------------------------------------------
[2002-06-17 14:46:14] rodif_bl@php.net
THIS IS BOGUS... It DOES NOT clear out all member variables...
if you look at the examples getA and getB return $this->$a and
$this->$b which $this->$a is just as undefined as $this->$undefined.
getA and getB should return $this->a and $this->b (with out the $).
$this->$undefined = "blah";
will assign '' to "blah";
return $this->$a;
is the exact same thing as
return $this->'';
so you are returning "blah"
if you change your 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";
?>
you will see it works fine!
------------------------------------------------------------------------
[2002-06-17 14:35:52] sander@php.net
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.
------------------------------------------------------------------------
[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'
------------------------------------------------------------------------
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