Re: Re: $this variable reference question

From: Date: Sat, 03 Apr 2004 04:07:44 +0000
Subject: Re: Re: $this variable reference question
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-182234@lists.php.net to get a copy of this message
Jason, thanks. Yes, I understand your example here very well. My question actually had more to do with understanding what value was added to the class by "$myref = &$this;". Perhaps it is some misunderstanding I have of classes but, since a class is just a pattern and not an actual, usable object in and of itself, what is the value in equating a member variable with the class. Almost seems recursive. Even though $myref is set to "global" it is still a member variable, right? Does "$this" refer to the class pattern or is it creating an object of itself inside the class, sort of an object inside an object? I guess my problem in understanding this is not so much where this sort of coding should be used but rather what is really going on here. Thanks again! -Adam Reiswig Jason Barnett wrote:
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 ?>


« previous php.general (#182234) next »