note 123495 added to language.oop5.references
| From: | gevorgmelkoumyan at gmail dot com | Date: | Sun, 06 Jan 2019 15:46:58 +0000 |
| Subject: | note 123495 added to language.oop5.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210015@lists.php.net to get a copy of this message | ||
I think this example should clarify the difference between PHP references (aliases) and pointers:
<?php
class A {
public $var = 42;
}
$a = new A; // $a points to the object with id=1
echo 'A: ' . $a->var . PHP_EOL; // A: 42
$b = $a; // $b points to the object with id=1
echo 'B: ' . $b->var . PHP_EOL; // B: 42
$b->var = 5;
echo 'B: ' . $b->var . PHP_EOL; // B: 5
echo 'A: ' . $a->var . PHP_EOL; // A: 5
$b = new A; // now $b points to the object with id=2, but $a still points to the 1st object
echo 'B: ' . $b->var . PHP_EOL; // B: 42
echo 'A: ' . $a->var . PHP_EOL; // A: 5
?>
----
Server IP: 208.43.231.9
Probable Submitter: 2a02:cb43:e::301 (proxied: 37.252.81.241)
----
Manual Page -- http://php.net/manual/en/language.oop5.references.php
Edit -- https://master.php.net/note/edit/123495
Del: integrated -- https://master.php.net/note/delete/123495/integrated
Del: useless -- https://master.php.net/note/delete/123495/useless
Del: bad code -- https://master.php.net/note/delete/123495/bad+code
Del: spam -- https://master.php.net/note/delete/123495/spam
Del: non-english -- https://master.php.net/note/delete/123495/non-english
Del: in docs -- https://master.php.net/note/delete/123495/in+docs
Del: other reasons-- https://master.php.net/note/delete/123495
Reject -- https://master.php.net/note/reject/123495
Search -- https://master.php.net/manage/user-notes.php