php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73476 Add weak references to PHP
Submitted: 2016-11-08 11:41 UTC Modified: 2019-11-27 17:07 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: php at haravikk dot me Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: 7.1.0RC5 OS:
Private report: No CVE-ID: None
 [2016-11-08 11:41 UTC] php at haravikk dot me
Description:
------------
So I've been using WeakRef for ages, but it's always struck me as odd that PHP doesn't support weak references to begin with.

I got so used to using WeakRef that I forgot entirely that I had to install it, so upgrading to PHP7 came as a shock when everything broke without it (and at the time it hadn't been updated to support PHP7).

It just seems like such a fundamental feature for any class-based language, especially one which typically runs in a tightly memory constrained environment.

So this request is that WeakRef be moved into the core PHP library, as it's such an important feature to have.


I know there is another issue #52318 asking for weak references (in 2010!) but it was closed under the assumption that the existence of the PECL extension is good enough; but I'm hoping to make the case that the extension should be rolled into PHP proper, as it's just such a crucial thing to have.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-13 19:29 UTC] krakjoe@php.net
-Status: Open +Status: Suspended
 [2016-11-13 19:29 UTC] krakjoe@php.net
This kind of change needs to go through the RFC process, so I'm suspending the bug.

It will not be removed, and should you, or anyone else choose to make an RFC you can link here for reference still.

On a side note; There are people that have gone their whole careers without needing to use weak references, I am one such person: It's a hard case to make that they are *required*.

What you can say is that their functionality is tightly coupled to the current implementation of ZE, and that moving them into the mainline development tree will ensure their compatibility for future versions. 

That may not be a very convincing argument, but it's not false. The argument that they are necessary is obviously false.
 [2016-11-13 21:13 UTC] php at haravikk dot me
> There are people that have gone their whole careers without needing to use weak references

There are also people who have used them throughout their careers; weak references let us do stuff like holding weak object references in a cache so that they can be pulled out only so long as they are still in use *somewhere* within the application, which I've found very useful (useful enough to need to install the PECL weakref module), as it means the object is still only occupying memory for so long as a reference is held to it, but it can be still be retrieved for re-use (especially handy if it's costly to create).

I've also found them useful in handling things like database results that need to be freed before new ones can be fetched; if I hold a weak reference to the last result retrieved, then I can know if it is still being held strongly somewhere in my application, and either produce an error, or trigger some kind of additional buffering so that it can be safely freed without breaking whatever code is still holding onto it (as it may still need to process it).

The WeakMap type of the PECL weakref module is likewise useful, as it allows associating of additional values for objects that I may not have control over, or be able to extend in a useful fashion (or just arbitrary objects in general); since the WeakMap entry expires when the object does, I never need to worry about them creating a memory leak.

There are loads of useful purposes to which to apply WeakRef and WeakMap, and there aren't many object-oriented languages that I'm aware of that don't have support for weak referencing; PHP is one of the few I know of that has that dubious distinction.
 [2019-11-27 17:06 UTC] cmb@php.net
-Status: Suspended +Status: Closed -Assigned To: +Assigned To: cmb
 [2019-11-27 17:06 UTC] cmb@php.net
WeakReferences[1] are available in the core as of PHP 7.4.0.
WeakMaps[2] have been suggested for inclusion for PHP 8.

[1] <https://www.php.net/weakreference>
[2] <https://wiki.php.net/rfc/weak_maps>
 [2019-11-27 17:07 UTC] cmb@php.net
-Package: Weakref +Package: *General Issues
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 07:01:27 2024 UTC