Weak References in PHP 7.4
Introduction
Weak references allow programmers to retain references to objects without This object does not prevent the object from being destroyed; they are useful for implementing cache-like structures. They currently have extended support in PHP.
Current implementations of WeakRef are implemented by overloading object handlers. The most popular implementation (pecl-weakref) changes the stack allocated to read-only memory, and PHP 7.3 will make it unavailable. Other implementations that change object handlers in other ways also run the risk of breaking and relying on undefined behavior and throwing away consistency.
Other implementations are possible, krakjoe/uref is an implementation that uses low-level features (mprotect, 0xCC, signals) to implement weakrefs, but at the cost of portability, generation of segmentation faults, protected memory and implicit Signal handler limitations.
In principle, weak reference objects are not complicated, just (ab)using Zend or the layer below, since we don't support it directly.
Recommendation
We directly support weak references in the simplest way.
API
The API:
final class WeakReference { public static function create(object $object) : WeakReference; public function get() : ?object; }
Backward-incompatible changes
None.
Translation: https://wiki.php.net/rfc/weakrefs
The above is the detailed content of Weak References in PHP 7.4. For more information, please follow other related articles on the PHP Chinese website!