Home >Backend Development >PHP Tutorial >php对象在内存中的存在形式分析_PHP

php对象在内存中的存在形式分析_PHP

WBOY
WBOYOriginal
2016-05-31 13:17:36892browse

本文实例分析了php对象在内存中的存在形式。分享给大家供大家参考。具体分析如下:

<&#63;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;
&#63;>

(1)$p1对应内存地址,假设是0x123,($p1和地址存放在栈区,相当于我们查字典时的索引);
(2)通过内存地址的索引,找到堆区。堆区里面存放着”小王“,”80“等数据
(3)$p2 = $p1,实际上是将$p1的内存地址0x123传给$p2,堆区里的属性$name,$age不变,也就是不会再重新复制一份。所以,在改变$p2->age=85时,$p1->age的值也改变了。

希望本文所述对大家的php程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn