PHP reference passing
Although I am also a learner of PHP, I really didn’t know much about the garbage collection process inside PHP before. We just used unset, null, mysql_close, __destruct and other functions in our code to release objects to prevent memory overflow, so I found the following instructions under GG on the Internet and made a record: "PHP can automatically manage memory and clear objects that are no longer needed. PHP uses a simple garbage collection mechanism called reference counting. Every time Each object contains a reference counter, and each reference is connected to the object. When the reference leaves the living space or is set to NULL, the counter is decremented by 1. When the reference counter of an object is zero, PHP knows that you will. This object is no longer needed and the memory space it occupies is released.”
As we all know, the PHP engine itself is written in C. When mentioning C, we must mention GC (garbage collection). Through the PHP manual, we know that the PHP engine will automatically perform GC actions. So we can’t help but ask, what exactly does it do? How is it recycled? Is the & reference operation a pointer? When unset() is used to unset() a variable, is it really recycled? These seem to be issues mentioned in the manual, but if you analyze them carefully, you will find that they are far from simple and general. . Maybe someone will jump out and say: You can know it by looking at the PHP source code. Yes, after you read through the PHP source code, this problem will definitely go away. However, this article will only analyze these seemingly ordinary but commonly used problems from PHP itself. I have overlooked the small details. Of course, it is inevitable that my level is limited and there are some omissions. I warmly welcome everyone.
phper to discuss together.
First of all, let’s see the example, the simplest execution process:
Example 1: gc.php
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
echo $b ." ";
?>
Needless to say% php -f gc.php The output result is very clear:
hy0kl% php -f gc.php
I am test.
OK, next:
Example 2:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = 'I will change?';
echo $a ." ";
echo $b ." ";
?>
The execution result is still obvious:
hy0kl% php -f gc.php
I will change?
I will change?
Please take a look :
Example 3:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
unset($a);
echo $a ." ";
echo $b ." ";
?>
Do you have to think about it?
hy0kl% php -f gc.php
Notice: Undefined variable: a in /usr/local/www/apache22/data/test /gc.php on line 8
I am test.
Are you a little confused?
Look again:
Example 4:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a ;
unset($b);
echo $a ." ";
echo $b ." ";
?>
In fact, if you understand Example 3, this is the same as Example 3.
hy0kl% php -f gc.php
I am test.
Notice: Undefined variable: b in /usr/local/www/apache22/data/test/gc.php on line 9
Look at:
Example 5:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$a = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
What is the first feeling of fierceness?
hy0kl% php -f gc.php
$a =
$b =
Yes, this is the output result. PHPers who have a deep understanding of PHP GC will not find it strange. To be honest, I was surprised when I ran this code for the first time, but it made me wonder. Now that you have a deeper understanding of PHP GC, the following example of working with it will naturally be easier to understand.
Example 6:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
OK, if the result of the above example does not have any details for the viewer, then you can close this window. Welcome to come back when you have time!
Let’s analyze GC and references in detail.
1. In all examples, a variable is created. In layman’s terms, this process is to open up a space in the memory and store a string I in it. am test.. There is a symbol table inside PHP to record the reference count of each block of memory. At this time, the reference count of this block of memory will be increased by 1, and a label (variable) named $a will point to this block of memory. , which makes it easy to operate the memory according to the tag name.
2. Perform & operation on variable $a. My understanding is to find the memory pointed by $a, establish the same reference point for $b, and store the memory block of the string I am test. in the symbol table. The reference count is increased by 1. In other words, when our script executes this line, the memory storing the string I am test. is referenced twice. What should be emphasized here is that the & operation establishes a reference point, not Pointers, PHP has no concept of pointers! At the same time, some people have proposed file soft links similar to UNIX. It can be understood to a certain extent: the memory storing the character I am test. is a real file of ours, and the variable $a and
$b is a soft link established for a real file, but they point to the same real file. So, we see that when $b is assigned a value in Example 2, the value of $a also changes. And through a certain A soft link operates the file similarly.
3. In Examples 3 and 4, the unset() operation was performed. According to the actual execution results, it can be seen that: unset() only disconnects the reference of the variable to the memory it originally pointed to, making the variable itself undefined. If a null reference is passed, a Notice is issued when the call is made, and the reference count of that piece of memory in the symbol table is decremented by 1, and it does not affect other variables pointing to this piece of memory. In other words, only when a piece of memory has a reference count in the symbol table When it is 0, the PHP engine will reclaim this memory.
PHP Manual
4.0.0 unset() became an expression. (In PHP 3, unset() would always return 1).
What does this mean?
Look at the following code and its result:
error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;
unset($a);
unset($a);
unset($a);
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
? >
hy0kl% php -f gc.php
Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 10
$a =
$b = I am test.
The first unset() operation has disconnected the pointer, so subsequent operations will not affect the reference count of any memory in the symbol table.
4. Through Example 5 & 6, it can be clearly concluded that the operation of assigning null is quite violent. It will directly set the reference count in the symbol number of the memory pointed to by the variable to 0. Then this memory will naturally be reclaimed by the engine. As for how It is unknown when it will be used again. It may be used to store other information immediately, and may never be used again. But in any case, all the original variables pointing to that memory will no longer be able to operate the reclaimed memory. Any attempt to Any variable calling it will return null.
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
if (null === $a)
{
echo '$a is null.';
} else
{
echo 'The type of $a is unknown.';
}
?>
hy0kl% php -f gc.php
$a =
$b =
$a is null.
In summary, it fully explains why we are looking at the source code of open source products Sometimes, we often see that some relatively large temporary variables, or reused information that is no longer called after use, will be concentrated or displayed as null. It is equivalent to directly killing the real file in UNIX, and all soft links pointing to it Naturally it became an empty chain
The above introduces the reference passing of PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
