Home > Backend Development > PHP Tutorial > Let everyone understand the knowledge of PHP weak references

Let everyone understand the knowledge of PHP weak references

藏色散人
Release: 2023-04-10 06:02:01
forward
2861 people have browsed it

This article will introduce you to the knowledge of PHP weak references. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

In the previous article, we have learned the knowledge related to references and reference-by-value. We know that there are no pure references (pointers) in PHP. Whether it is an object or a variable assigned with the reference symbol &, it is a reference to a symbol table. Today, we are going to learn about another form of reference: weak reference.

What is a weak reference

Weak references allow the programmer to retain a reference to an object without preventing the object from being destroyed; they are useful for implementing cache-like structures.

This is a more official explanation. From this explanation, we can see that weak reference is also a form of reference, but if we destroy the original object, the weak reference object will also be destroyed, just like ordinary value object assignment. If you have not read the previous article, or are not familiar with references in PHP, you may need to learn more about references in PHP. Below, let’s take a look directly through an example.

WeakReference

$obj = new stdClass;
$weakref = $obj;

var_dump($weakref);
// object(stdClass)#1 (0) {
// }

unset($obj);
var_dump($weakref);
// object(stdClass)#1 (0) {
// }

$obj1 = new stdClass;
$weakref = WeakReference::create($obj1);

var_dump($weakref->get());
// object(stdClass)#2 (0) {
// }

unset($obj1);
var_dump($weakref->get());
// NULL

$weakref = WeakReference::create(new stdClass);
var_dump($weakref->get());
// NULL
Copy after login

The first object \$obj we perform direct assignment reference, which is PHP's default object assignment. At this time, $weakref holds a reference to the object symbol table. When we unset() $obj, $weakref can still be used normally. In other words, $weakref's memory reference to the original object $obj still remains. No matter how we unset() the original $obj, it only cuts off the reference symbol table of $obj and has no effect on the real object. The garbage collector will not completely recycle the contents of the original $obj object.

For the second object, we use the create() method of WeakReference to create a weak reference. When we destroy $obj1, $weakref will also become NULL. This is what weak references are for!

It allows the garbage collector to recycle normally, it can avoid memory leaks caused by circular references, and it can make references behave similarly to pointer operations in C.

The last piece of code is that we use new directly in WeakReference::create() to create the object. This form is not possible and will always return NULL. Because a weak reference is created through a variable, it points to the symbol table of the original object, and the symbol table connection between the variable and the object is what the weak reference cares about. It will judge the current status based on the status of the symbol table. . If the original object variable is cut off from the symbol table, the weak reference variable will also be cut off synchronously, so that the garbage collector can normally clean up the object that no longer has any reference count.

Note

It should be noted here that the above test code must be used in PHP7.4 and above. The WeakReference class is a new addition to PHP7.4. Previous versions required the WeakRef extension to be installed to implement weak reference capabilities. For specific content, please refer to the relevant documents in the link below.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202006/source/%E5%AD%A6%E4%B9%A0PHP%E5%BC%B1%E5%BC%95%E7%94%A8%E7%9A%84%E7%9F%A5%E8%AF%86.php
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of Let everyone understand the knowledge of PHP weak references. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
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