Home > Backend Development > PHP7 > body text

Weak References in PHP 7.4

藏色散人
Release: 2023-02-17 13:20:02
Original
3384 people have browsed it

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;
}
Copy after login

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!

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