In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve weak references in PHP. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
What is a weak reference?
Weak references allow programmers to retain references to objects that do not prevent them from being destroyed; they are useful for implementing cache-like structures.
This is a more official explanation. From this description, we can see that a 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 a normal value object assignment. If you haven't read the previous article, or if you're not familiar with references in PHP, you may need to know more about references in PHP. Next, let's look at it 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
For the first object,\ $obj, we make a direct assignment reference, which is the default object assignment for PHP. At this point, $weakref holds a reference to the object symbol table. When we unset () drop $obj, $weakref still works. That is, the memory reference of $weakref to the original $obj object remains. No matter how we unset () the original $obj, it just cuts off the reference symbol table of $obj, has no effect on the real object, and the garbage collector does not completely recycle the original $obj object content.
For the second object, we use WeakReference's create () method to create a weak reference, and when we destroy $obj1, $weakref also becomes NULL. This is the function of weak quotation!
It allows the garbage collector to collect normally, it avoids memory leaks caused by circular references, and it makes references behave like pointer operations in C #.
The last piece of code is that we create the object directly using new in WeakReference::create (). This form will not work, it will always return NULL. Because weak references are created by variables, it points to the symbol table of the original object, and the symbol table connection between variables and objects is what the weak reference cares about, which determines the current state according to the state of the symbol table. If the original object variable is severed from the symbol table, the weakly referenced variables are also cut off synchronously, so that the garbage collector can clean up the object without any reference count.
After reading the above, do you have any further understanding of how to implement weak references in PHP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.