Objects & References to objects: Can't get them to work

From: Date: Tue, 08 Aug 2000 15:04:14 +0000
Subject: Objects & References to objects: Can't get them to work
Groups: php.general 
Request: Send a blank email to php-general+get-10721@lists.php.net to get a copy of this message
Kind folks, (I'm new to the list so I'm sorry if you've covered this one already.) I'm wondering if someone can enlighten me on how references work in particular WRT objects. Basically, I want to create an object and use that instance in many different parts of the page code. So, if I create an object over here and then pass it over there. I want both places to refer to the same object. I find that I'm always getting copies of objects rather than references to the one instance. Strangely, at least to me, If I use the '&' operator (e.g. &$foo) I get variable references rather than data references. Here's example code to show you what I'm talking about. Any help would be appreciated. Lo. <?PHP /* Class A, just a dummy class that holds an id so that we know which * object is which. */ class A {
    var $id;
    function A ($anId) {
        $this->id = $anId;
    }
} /* Class B, here's a class that test different ways of passing objects to * functions/methods. */ class B {
    var $bar;
    function foo1 ( $anObject) { $this->bar =  $anObject; }
    function foo2 ( $anObject) { $this->bar = &$anObject; }
    function foo3 (&$anObject) { $this->bar =  $anObject; }
    function foo4 (&$anObject) { $this->bar = &$anObject; }
} /* To start out with, let's create a dummy object of class A. */ $x = new A("abc"); print "\$x->id = " . $x->id . "<br>\n"; // Prints "$x->id = abc" as expected /* TEST 1: A plain assignment makes copies of objects */ print "<b>TEST 1: </b><br>\n"; $y = $x;
$x->id = "xyz";                           // Plain assignment
print "\$x->id = " . $x->id . "<br>\n"; // Prints "$x->id = xyz" as expected print "\$y->id = " . $y->id . "<br>\n"; // Prints "$y->id = abc" !!! /* TEST 2: */ print "<b>TEST 2: </b><br>\n"; $z = new B; $x->id = "NO"; $z->foo1($x); print "foo1(): z starts out with: " . $z->bar->id . "<br>\n"; $x->id = "YES";
print "Did it change? " . $z->bar->id . "<br>\n";           // Says "NO"
$x->id = "NO"; $z->foo2($x); print "foo2(): z starts out with: " . $z->bar->id . "<br>\n"; $x->id = "YES";
print "Did it change? " . $z->bar->id . "<br>\n";           // Says "NO"
$x->id = "NO"; $z->foo3($x); print "foo3(): z starts out with: " . $z->bar->id . "<br>\n"; $x->id = "YES";
print "Did it change? " . $z->bar->id . "<br>\n";           // Says "NO"
$x->id = "NO"; $z->foo4($x); print "foo1(): z starts out with: " . $z->bar->id . "<br>\n"; $x->id = "YES";
print "Did it change? " . $z->bar->id . "<br>\n";           // Says "YES" !
/* Finally, it changed but let's see if it's actually referencing the variable * $x instead just referencing the object. (TEST 3) */ print "<b>TEST 3: </b><br>\n"; $z->foo1(new A("This shouldn't change \$x but it does.")); print "\$x->id = " . $x->id . "<br>\n"; $x = new A("This should't change \$z but it does."); print "\$z->bar->id = " . $z->bar->id . "<br>\n"; /* TEST 4. And another thing, I can't use scalars either with foo4(). */ print "<b>TEST 4: </b><br>\n"; $z->foo4("This will raise a fatal error."); ?>

« previous php.general (#10721) next »