变量改变时PHP内核做了些什么?
引言
内容来自于《Extending and Embedding PHP》- Chaper 3 - Memory Management,加上自己的理解,对php中变量的引用计数、写时复制,写时改变,写时复制和改变做个”翻译“。
zval
看下面的内容之前先对zval这个结构体做个了解
<code class="hljs thrift" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 255);">typedef</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 255);">struct</span> _<span class="hljs-title" style="color: rgb(163, 21, 21);">zval_struct</span> </span>{ zvalue_value value; zend_uint refcount; zend_uchar type; zend_uchar is_ref; } zval;</code>
zval结构体中共有4个元素,value是一个联合体,用来真正的存储zval的值,refcount用来计数该zval被多少个变量使用,type表示zval所存储的数据类型,is_ref用来标志该zval是否被引用。
引用计数
<code class="hljs xml" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"><span class="php"><span class="hljs-preprocessor" style="color: rgb(43, 145, 175);"><?php </span> <span class="hljs-variable">$a</span> = <span class="hljs-string" style="color: rgb(163, 21, 21);">'Hello World'</span>; <span class="hljs-variable">$b</span> = <span class="hljs-variable">$a</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 255);">unset</span>(<span class="hljs-variable">$a</span>); <span class="hljs-preprocessor" style="color: rgb(43, 145, 175);">?></span></span></span></code>
我们一起来剖析下上面这段代码:
-
$a = 'Hello World';
首先这句代码被执行,内核创建一个变量,并分配12字节的内存去存储字符串'Hello World'和末尾的NULL。 -
$b = $a;
接着执行这句代码,执行这句的时候内核里面发生了什么呢?- 对
$a
所指向的zval中的refcount进行加1操作。 -
将变量
$b
指向$a
所指向的zval。
在内核中大概是这样的,其中active_symbol_table
是当前的变量符号表<code class="hljs clojure" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-collection">{ zval *helloval; MAKE_STD_ZVAL<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">helloval</span>)</span><span class="hljs-comment" style="color: green;">;</span> ZVAL_STRING<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">helloval</span>, <span class="hljs-string" style="color: rgb(163, 21, 21);">"Hello World"</span>, <span class="hljs-number">1</span>)</span><span class="hljs-comment" style="color: green;">;</span> zend_hash_add<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">EG</span><span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">active_symbol_table</span>)</span>, <span class="hljs-string" style="color: rgb(163, 21, 21);">"a"</span>, sizeof<span class="hljs-list">(<span class="hljs-string" style="color: rgb(163, 21, 21);">"a"</span>)</span>, &helloval, sizeof<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">zval*</span>)</span>, NULL)</span><span class="hljs-comment" style="color: green;">;</span> ZVAL_ADDREF<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">helloval</span>)</span><span class="hljs-comment" style="color: green;">;</span> zend_hash_add<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">EG</span><span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">active_symbol_table</span>)</span>, <span class="hljs-string" style="color: rgb(163, 21, 21);">"b"</span>, sizeof<span class="hljs-list">(<span class="hljs-string" style="color: rgb(163, 21, 21);">"b"</span>)</span>, &helloval, sizeof<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 255);">zval*</span>)</span>, NULL)</span><span class="hljs-comment" style="color: green;">;</span> }</span></code>
- 对
-
unset($a);
这句代码执行后,内核会将a对应的zval结构体中的refcount计数减一, b还和原来一样
写时复制
<code class="hljs xml" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"><span class="php"><span class="hljs-preprocessor" style="color: rgb(43, 145, 175);"><?php </span> <span class="hljs-variable">$a</span> = <span class="hljs-number">1</span>; <span class="hljs-variable">$b</span> = <span class="hljs-variable">$a</span>; <span class="hljs-variable">$b</span> += <span class="hljs-number">5</span>; <span class="hljs-preprocessor" style="color: rgb(43, 145, 175);">?></span></span></span></code>
上面这段代码执行完之后,一般肯定希望$a=1,$b=6
,但是如果像引用计数那样,$a
和$b
指向相同的zval,修改$b
之后$a
不是也变了?
这个具体是怎么实现的呢,我们一起来看下:
-
$a = 1;
内核创建一个zval,并分配4个字节存储数字1。 -
$b = $a;
这一步和引用计数中的第二步一样,将$b
指向和$a
相同的zval,并将zval中的引用计数值refcount加1。 -
$b += 5;
关键是这一步,这一步骤发生了什么呢,怎么确保修改之后不影响$a
。- 其实Zend内核在改变zval之前都会去进行
get_var_and_separete
操作,如果recfount>1,就需要分离就创建新的zval返回,否则直接返回变量所指向的zval,下面看看如何分离产生新的zval。 - 复制一个和
$b
所指向zval一样的zval。 - 将
$b
所指向的zval中的refcount计数减1。 - 初始化生成的新zval,设置refcount=1,is_ref=0。
- 让
$b
指向新生成的zval。 -
对新生成的zval进行操作,这就是写时复制。
下面看看内核中分离时的主要代码:<code class="hljs lasso" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"> zval <span class="hljs-subst">*</span>get_var_and_separate(char <span class="hljs-subst">*</span>varname, int varname_len TSRMLS_DC) { zval <span class="hljs-subst">**</span>varval, <span class="hljs-subst">*</span>varcopy; <span class="hljs-keyword" style="color: rgb(0, 0, 255);">if</span> (zend_hash_find(EG(active_symbol_table), varname, varname_len <span class="hljs-subst">+</span> <span class="hljs-number">1</span>, (<span class="hljs-literal">void</span><span class="hljs-subst">**</span>)<span class="hljs-subst">&</span>varval) <span class="hljs-subst">==</span> FAILURE) { <span class="hljs-comment" style="color: green;">/* Variable doesn't actually exist fail out */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 255);">return</span> <span class="hljs-built_in" style="color: rgb(0, 0, 255);">NULL</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 255);">if</span> ((<span class="hljs-subst">*</span>varval)<span class="hljs-subst">-></span>is_ref <span class="hljs-subst">||</span> (<span class="hljs-subst">*</span>varval)<span class="hljs-subst">-></span>refcount <span class="hljs-subst"> <span class="hljs-number">2</span>) { <span class="hljs-comment" style="color: green;">/* varname is the only actual reference, * or it's a full reference to other variables * either way: no separating to be done */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 255);">return</span> <span class="hljs-subst">*</span>varval; } <span class="hljs-comment" style="color: green;">/* Otherwise, make a copy of the zval* value */</span> MAKE_STD_ZVAL(varcopy); varcopy <span class="hljs-subst">=</span> <span class="hljs-subst">*</span>varval; <span class="hljs-comment" style="color: green;">/* Duplicate any allocated structures within the zval* */</span> zval_copy_ctor(varcopy); <span class="hljs-comment" style="color: green;">/* Remove the old version of varname * This will decrease the refcount of varval in the process */</span> zend_hash_del(EG(active_symbol_table), varname, varname_len <span class="hljs-subst">+</span> <span class="hljs-number">1</span>); <span class="hljs-comment" style="color: green;">/* Initialize the reference count of the * newly created value and attach it to * the varname variable */</span> varcopy<span class="hljs-subst">-></span>refcount <span class="hljs-subst">=</span> <span class="hljs-number">1</span>; varcopy<span class="hljs-subst">-></span>is_ref <span class="hljs-subst">=</span> <span class="hljs-number">0</span>; zend_hash_add(EG(active_symbol_table), varname, varname_len <span class="hljs-subst">+</span> <span class="hljs-number">1</span>, <span class="hljs-subst">&</span>varcopy, sizeof(zval<span class="hljs-subst">*</span>), <span class="hljs-built_in" style="color: rgb(0, 0, 255);">NULL</span>); <span class="hljs-comment" style="color: green;">/* Return the new zval* */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 255);">return</span> varcopy; }</span></code>
- 其实Zend内核在改变zval之前都会去进行
写时改变
<code class="hljs xml" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"><span class="php"><span class="hljs-preprocessor" style="color: rgb(43, 145, 175);"><?php </span> <span class="hljs-variable">$a</span> = <span class="hljs-number">1</span>; <span class="hljs-variable">$b</span> = &<span class="hljs-variable">$a</span>; <span class="hljs-variable">$b</span> += <span class="hljs-number">5</span>; <span class="hljs-preprocessor" style="color: rgb(43, 145, 175);">?></span></span></span></code>
上面这段代码执行完之后一般希望是:$a == $b == 6
。这个又是怎么实现的呢?
-
$a = 1;
这一步骤和写时复制中的第一步一样。 -
$b = &$a;
这一步骤内核会将$b
指向$a
所指向的zval,将zval中的refcount加1,并将zval中的is_ref置为1。 -
$b += 5;
这一步骤和写时复制中的第三步骤一样,但是内核中发生的事情却不一样。- 内核看到
$b
进行变化的时候,也会执行get_var_and_separate函数,看是否需要分离。 - 如果
(*varval)->is_ref
的话也会直接返回$b
所指向的zval,不去分离产生新的zval,不管zval的refcount是否>1。 - 这时候再去修改
$b
值,$a
的值也就改变了,因为他们指向相同的zval。
- 内核看到
分离的问题
说道现在聪明的你可能已经看出点问题了,如果一个zval结构体既有refcount计数又有is_ref引用这个时候怎么办?
<code class="hljs xml" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"><span class="php"><span class="hljs-preprocessor" style="color: rgb(43, 145, 175);"><?php </span> <span class="hljs-variable">$a</span> = <span class="hljs-number">1</span>; <span class="hljs-variable">$b</span> = <span class="hljs-variable">$a</span>; <span class="hljs-variable">$c</span> = &<span class="hljs-variable">$a</span>; <span class="hljs-preprocessor" style="color: rgb(43, 145, 175);">?></span></span></span></code>
如果出现上面这种情况的时候,如果$a、$b、$c
指向同一个zval结构体,进行改变的时候Zend到底去听谁的?其实这个地方不会指向同一个zval了。
如果对一个is_ref = 0 && refcount >1
的zval进行写时改变这种赋值形式(就是引用赋值)的时候,Zend会将等号右边的变量分离出来一个新的zval,
对这个zval进行初始化,对之前的zval的refcount进行减1操作,让等号左边的变量指向这个新的zval,refcount进行加1操作,is_ref=1。看看下面这张图片
<code class="hljs xml" style="font-family: 'Courier New', sans-serif !important; line-height: 1.5 !important; font-size: 12px !important; background-color: rgb(245, 245, 245) !important; border: 1px solid rgb(204, 204, 204) !important; padding: 5px !important; border-top-left-radius: 3px !important; border-top-right-radius: 3px !important; border-bottom-right-radius: 3px !important; border-bottom-left-radius: 3px !important; display: block; overflow-x: auto; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;"><span class="php"><span class="hljs-preprocessor" style="color: rgb(43, 145, 175);"><?php </span> <span class="hljs-variable">$a</span> = <span class="hljs-number">1</span>; <span class="hljs-variable">$b</span> = &<span class="hljs-variable">$a</span>; <span class="hljs-variable">$c</span> = <span class="hljs-variable">$a</span>; <span class="hljs-preprocessor" style="color: rgb(43, 145, 175);">?></span></span></span></code>
上面这又是另外一种情况,在is_ref = 1
的情况下,试图单纯的进行refcount+1操作的时候会分离出来一个新的zval给等号左边的变量,并初始化他,看看下面这张图片
参考文献
1.《Extending and Embedding PHP》- Chaper 3 - Memory Management.

在PHP开发中,我们经常会遇到PHPNotice:Undefinedvariable的错误提示。这个错误提示表示我们在代码中使用了一个未定义的变量。虽然这个错误提示不会导致代码崩溃,但是它会影响代码的可读性和可维护性。下面,本文将为大家介绍一些解决这个错误的方法。1.在开发过程中使用error_reporting(E_ALL)函数在PHP开发中,我们可

PHPNotice:Undefinedvariable:arrin的解决方法在PHP编程中,我们经常会遇到“Notice:Undefinedvariable”这个错误提示。这个错误提示一般是因为访问了未定义的变量或者变量未被初始化导致的。对于这个问题,我们需要及时找到问题并解决。在本文中,我们将重点讨论PHPNotice:Undefin

PHPNotice:Undefinedvariable:result是指在PHP程序中调用了一个未定义的变量result,这会导致程序产生Notice级别的警告。这种情况一般是由于程序员在编写PHP代码时未正确定义变量或者变量的作用域造成的。如果不及时解决,这种Notice级别的警告可能会导致程序的运行出现问题。那么,如何解决PHPNotice:

在PHP中,您可以使用和号(&)符号将变量按引用而不是按值传递。这样可以在函数或方法内修改原始变量。主要有两种方式可以通过引用传递PHP变量:使用ampersand符号在函数/方法声明中使用和符号将变量传递给函数/方法时在函数/方法声明中使用和号在PHP中,您可以使用函数/方法声明中的和号符号(&)通过引用传递变量。以下是更新的解释:要通过在函数/方法声明中使用&符号来传递引用变量,您需要在函数/方法定义中在参数名称之前包含&符号。这表示参数应该通过引用传递,允许

在开发PHP应用程序时,如果遇到了"Undefinedvariable:sql"的提示,这通常意味着您正在引用一个未定义的变量。这可能是由于许多原因引起的,例如变量名称拼写错误、作用域问题或代码中的语法错误等。在本篇文章中,我们将探讨这个问题的各种原因,并提供一些解决这个问题的方法。1.变量名称拼写错误在您的PHP代码中,如果变量名称不正确或拼写错误,系

PHP7底层开发原理入门指南:从零开始学习PHP内核的奥秘引言:随着互联网的迅猛发展,PHP作为一种流行的服务器端脚本语言,具备了广泛的应用场景。然而,很多人对于PHP的内部原理和工作原理却知之甚少。对于想要深入了解PHP内核的开发者来说,本文将提供一个入门指南,帮助他们从零开始学习PHP内核的奥秘。一、PHP内核的基本概念PHP的编译过程在PHP的编译过程

如何在PHP中使用数字变量在PHP中,数字变量是一种无需声明而直接使用的变量类型。可以使用数字变量进行数学计算、数据比较和其他数值操作。本文将介绍如何在PHP中使用数字变量,并提供具体的代码示例。定义数字变量在PHP中,定义数字变量非常简单,只需直接给变量赋予一个数字即可。下面是一个例子:$number=10;在上面的代码中,我们定义了一个名为$numb

PHP7底层开发原理详细介绍:了解PHP内核架构和组件的关系PHP是一种广泛使用的服务器端脚本语言,它可以嵌入到HTML中,用于开发动态网页。PHP的优势在于它简单易学,且与多种数据库集成得很好。对于很多开发者来说,他们都希望深入了解PHP底层的开发原理,以便更好地利用PHP提供的强大功能。本文将详细介绍PHP7底层开发原理,帮助读者深入理解PHP的内核架构


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!
