In previous articles, we learned about references and reference-passing. As we know, there are no pure references (Pointers) in PHP. Objects and variables assigned with the reference symbol & are references to a symbol table. Today, we’re going to look at a different type of citation: weak citation.

What is a weak reference

Weak references allow the programmer to preserve a reference to an object that does not prevent the object from being destroyed; They are useful for implementing cache-like structures.

That’s the official explanation. From this specification, we can see that a weak reference is also a form of reference, but if we destroy the original object, the weak reference object is also destroyed, just like a normal value object assignment. If you haven’t read the previous article, or are not familiar with PHP references, you may want to learn more about PHP references. Let’s look directly at 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 the code

For the first object, \ obj, we make a direct assignment reference, which is PHP’s default object assignment. In this case, obj we do a direct assignment reference, which is PHP’s default object assignment. In this case, obj we do a direct assignment reference, which is PHP’s default object assignment. In this case, WeakRef keeps a reference to the object’s symbol table. When we unset() to drop OBj, obj, obj, WeakRef can still be used normally. That is, weakRef to WeakRef to WeakRef’s memory reference to obj’s original object remains. No matter how unset() we unset the original obj, we just cut off obj, we just cut off obj’s reference symbol table, we have no effect on the real object, and the garbage collector will not completely reclaim the original $obj object content.

For the second object, we use WeakReference’s create() method to create a WeakReference. When we destroy obj1, obj1, and obj1, weakref will also become NULL. That’s what weak references are for!

It allows the garbage collector to collect properly, it avoids memory leaks from circular references, and it allows references to behave like pointer operations in C.

The last piece of code is that we use new directly in WeakReference:: Create () to create the object. This will not work and will always return NULL. Because weak references are created by variables, they point to the symbol table of the original object, and the symbol table connection between variables and objects is what weak references care about. It will judge the current state according to the symbol table state. If the original object variable is cut off from the symbol table, then the weakly referenced variable is also cut off synchronously, so that the garbage collector can clean up the object without any reference count.

Pay attention to

It is important to note that the above test code must be available in PHP7.4 and above, and the WeakReference class is a new addition to PHP7.4. The previous version required the installation of WeakRef extension to achieve the ability of weak reference. For details, please refer to the relevant documents in the link below.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/en/c…

www.php.net/manual/zh/b…