note 102242 added to language.oop5.references

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.