The example of this article analyzes the existence form of PHP objects in memory. Share it with everyone for your reference. The specific analysis is as follows:
<?php class Person{ public $name; public $age; } $p1 = new Person(); $p1->name = "小明"; $p1->age=80; $p2=$p1; $p2->age=85; echo $p2->name; echo $p1->age; ?>
(1) $p1 corresponds to the memory address, assuming it is 0x123, ($p1 and the address are stored in the stack area, which is equivalent to the index when we look up the dictionary);
(2) Find the heap area through the index of the memory address. The heap area stores data such as "Xiao Wang" and "80"
(3) $p2 = $p1, in fact, the memory address 0x123 of $p1 is passed to $p2. The attributes $name and $age in the heap area remain unchanged, that is, they will not be copied again. Therefore, when changing $p2->age=85, the value of $p1->age also changes.
I hope this article will be helpful to everyone’s PHP programming design.