weakrefs View as Markdown

Implemented (PHP 7.4)
Authors
krakjoe
Date
2018-05-17
Version
0.1
Source
wiki.php.net/rfc/weakrefs

Vote

Implement weakrefs ? closed
Yes
28
No
5
33 voters
ajf Yes 2019-03-06 00:25
ashnazg Yes 2019-02-26 15:17
bishop Yes 2019-02-22 14:34
bwoebi Yes 2019-02-22 12:35
carusogabriel Yes 2019-03-04 01:52
cmb Yes 2019-02-23 10:17
cpriest Yes 2019-02-25 12:08
derick No 2019-03-05 12:48
diegopires Yes 2019-02-22 17:54
duncan3dc Yes 2019-02-25 18:03
emir Yes 2019-03-03 07:13
galvao Yes 2019-02-22 22:33
gasolwu Yes 2019-02-25 03:12
hywan Yes 2019-02-22 12:25
jhdxr Yes 2019-02-26 18:31
kalle Yes 2019-02-25 13:58
kguest Yes 2019-02-28 14:07
krakjoe Yes 2019-02-22 11:54
laruence No 2019-03-05 02:11
lex Yes 2019-03-03 12:13
lstrojny Yes 2019-03-01 07:05
malukenho Yes 2019-02-25 13:17
mike Yes 2019-02-27 09:48
nikic Yes 2019-02-23 08:37
ocramius No 2019-02-26 17:51
pauloelr Yes 2019-02-22 19:20
petk Yes 2019-03-02 14:12
pollita No 2019-02-28 04:24
ramsey Yes 2019-02-22 16:05
rasmus Yes 2019-02-26 22:44
rtheunissen Yes 2019-02-26 23:32
sebastian No 2019-02-26 13:29
trowski Yes 2019-02-26 15:29

Discussions on the mailing list

RFC WeakRefs
by Joe Watkins · 9 messages
RFC Weakrefs
by Joe Watkins · 20 messages
[RFC][Vote] Weakrefs
by Joe Watkins · 3 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

Weak References allow the programmer to retain a reference to an object which does not prevent the object from being destroyed; They are useful for implementing cache like structures. They are currently supported in PHP by extension.

The current implementations of WeakRef all work by overloading object handlers, the most popular implementation (pecl-weakref) changes stack allocated read only memory and will be rendered unusable by PHP 7.3. Other implementations that change object handlers in some other way also risk faulting and rely on undefined behaviour and casting away constness.

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, generating segfaults, protecting memory, and implicit signal handler restrictions.

In principle weak referencing an object is not complicated, and only requires the (ab)use of Zend or layers beneath because we do not directly support it.

Proposal

We directly support weak referencing in the simplest possible way.

API

The proposed API:

final class WeakReference {
    public static function create(object $object) : WeakReference;
 
    public function get() : ?object;
}

The proposed API differs from the documented WeakRef class. The currently documented API includes the following methods omitted from the proposed API:

  • valid
  • acquire
  • release

Acquire and release are sugar for get and unset, and valid is simply superfluous since the get method will not throw an exception and null is a falsy value.

Implementation Details

The proposed API:

  • is closed
  • does not support serialization
  • does not support properties

Backward Incompatible Changes

None

Proposed PHP Version(s)

PHP 7.4

RFC Impact

To SAPIs

None

To Existing Extensions

None

To Opcache

None

New Constants

None

php.ini Defaults

None

Open Issues

None

Unaffected PHP Functionality

All

Proposed Voting Choices

Simple Yes/No, requires super majority.

Vote: Implement weakrefs ? — Yes: 28 · No: 5 (closed)

Voting started 2019-02-22, ends 2019-03-08.

Patches and Tests

https://github.com/php/php-src/pull/3787

References

Announce: https://externals.io/message/102111

Re-announce: https://externals.io/message/104014