note 124990 added to language.oop5.references
| From: | rnealxp.yahoo.com via php-notes | Date: | Sat, 09 May 2020 15:36:22 +0000 |
| Subject: | note 124990 added to language.oop5.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214598@lists.php.net to get a copy of this message | ||
Two concise examples that illustrate the true difference between "object-assignment by
value" vs "object-assignment by reference":
<?php
class a{
public $a = 2;
}
class b{
public $b = 3;
}
//------------
//Code Set 1 (third line down does not use ampersand(&), it is an object assignment by copy):
$a = new a();
$b = new b();
$c = $a; //<--copy by value.
echo $c->a . "\n"; //output: 2
$a = $b; //This assignment has no affect on $c.
echo $c->a . "\n"; //output: 2 ($c is still a copy of $a, so this works)
echo $c->b . "\n"; //output: "Undefined property: a::$b" ($c is still a copy
of $a (not $b), so this does not work)
//------------
//Code Set 2 (third line down DOES use ampersand(&), it is an object assignment by
reference/pointer):
$a = new a();
$b = new b();
$c = &$a; //<--copy by reference/pointer.
echo $c->a . "\n"; //output: 2
$a = $b; //This assignment replaces $c's underlying object value.
echo $c->a . "\n"; //output: "Undefined property: b::$a" ($c is no longer a
copy of $a, so this does not work)
echo $c->b . "\n"; //output: 3 ($c now points to $b, so this works)
//------------
?>
----
Server IP: 45.112.84.4
Probable Submitter: 38.142.137.34 (proxied: 96.61.135.240)
----
Manual Page -- http://php.net/manual/en/language.oop5.references.php
Edit -- https://master.php.net/note/edit/124990
Del: integrated -- https://master.php.net/note/delete/124990/integrated
Del: useless -- https://master.php.net/note/delete/124990/useless
Del: bad code -- https://master.php.net/note/delete/124990/bad+code
Del: spam -- https://master.php.net/note/delete/124990/spam
Del: non-english -- https://master.php.net/note/delete/124990/non-english
Del: in docs -- https://master.php.net/note/delete/124990/in+docs
Del: other reasons-- https://master.php.net/note/delete/124990
Reject -- https://master.php.net/note/reject/124990
Search -- https://master.php.net/manage/user-notes.php