note 102242 added to language.oop5.references
| From: | RobMarscher at osu1 dot php dot net | Date: | Thu, 03 Feb 2011 17:33:27 +0000 |
| Subject: | note 102242 added to language.oop5.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176651@lists.php.net to get a copy of this message | ||
Here's an example I created that helped me understand the difference between passing objects by
reference and by value in php 5.
<?php
class A {
public $foo = 'empty';
}
class B {
public $foo = 'empty';
public $bar = 'hello';
}
function normalAssignment($obj) {
$obj->foo = 'changed';
$obj = new B;
}
function referenceAssignment(&$obj) {
$obj->foo = 'changed';
$obj = new B;
}
$a = new A;
normalAssignment($a);
echo get_class($a), "\n";
echo "foo = {$a->foo}\n";
referenceAssignment($a);
echo get_class($a), "\n";
echo "foo = {$a->foo}\n";
echo "bar = {$a->bar}\n";
/*
prints:
A
foo = changed
B
foo = empty
bar = hello
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 66.11.209.170
----
Manual Page -- http://www.php.net/manual/en/language.oop5.references.php
Edit -- https://master.php.net/note/edit/102242
Del: integrated -- https://master.php.net/note/delete/102242/integrated
Del: useless -- https://master.php.net/note/delete/102242/useless
Del: bad code -- https://master.php.net/note/delete/102242/bad+code
Del: spam -- https://master.php.net/note/delete/102242/spam
Del: non-english -- https://master.php.net/note/delete/102242/non-english
Del: in docs -- https://master.php.net/note/delete/102242/in+docs
Del: other reasons-- https://master.php.net/note/delete/102242
Reject -- https://master.php.net/note/reject/102242
Search -- https://master.php.net/manage/user-notes.php