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

?>


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to