Re: $this variable reference question
| From: | Jason Barnett | Date: | Sat, 03 Apr 2004 01:13:33 +0000 |
| Subject: | Re: $this variable reference question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-182223@lists.php.net to get a copy of this message | ||
It's not the same thing. When an variable references an object then you can change either the original object or the new variable and you will be changing the *same* object, not a new one. Maybe this example helps?
<?php
class test {
function test($val) {
global $myref;
$myref = &$this;
$this->value = $val;
}
function printval() {
echo "The value of this class is '{$this->value}' <br>";
}
function setval($val) {
$this->value = $val;
}
}
$a = new test();
$b = &$a;
$c = new test();
$a->setval(22);
$b->setval(321);
$c->setval('A weird comment');
$a->printval(); // should see 321
$b->printval(); // should see 321
$c->printval(); // should see A weird comment
?>