Reference counters and copy-on-write for php variables

WBOY
Release: 2016-08-08 09:19:29
Original
1141 people have browsed it

As we all know, PHP does not support pointers, but what if you want two variables to point to the same memory block at the same time? In order to solve this problem, reference counters are used in the PHP kernel.
The previous blog post introduced how PHP variables are stored in the kernel. The following two member variables in the zval structure are used for reference counters:

<code>is_ref  BOOL值,标识变量是否是引用集合。
refcount  计算指向引用集合的变量个数。
</code>
Copy after login

Look at the PHP code below

<code><?php
    $a = "this is a";
?>
</code>
Copy after login

An entity of a zval structure is called a zval container. Creating a variable in the PHP language layer will correspondingly create a zval container in the PHP kernel. Because the above code creates a variable $a, a zval container will be created in the PHP kernel; and because this variable is not a reference, the is_ref of the zval container is equal to FALSE, and the refcount is equal to 1.
Look at the code below

<code><?php
    $a = "this is a";
    $b=$a;
?>
</code>
Copy after login

The above code creates two variablesa and

Copyright Disclaimer: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the reference counter and copy-on-write of PHP variables, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template