Bug #80951 [Opn->Nab]: WeakReferences are collected on scope

From: Date: Mon, 12 Apr 2021 16:34:41 +0000
Subject: Bug #80951 [Opn->Nab]: WeakReferences are collected on scope
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233373@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80951&edit=1

 ID:                 80951
 Updated by:         nikic@php.net
 Reported by:        alex dot a dot pott at gmail dot com
 Summary:            WeakReferences are collected on scope
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   OS X / Linux
 PHP Version:        8.0.3
 Block user comment: N
 Private report:     N

 New Comment:

WeakReferences have no impact on the object lifecycle at all -- that's their whole point. The
object will be destroyed at exactly the same time it would be destroyed if it had no weak
references.

I couldn't find the wording you quoted in the accepted RFC (https://wiki.php.net/rfc/weakrefs).
Apparently it was part of an older RFC (https://wiki.php.net/rfc/weakreferences).

Note that the canonical use-case for weak references is in the form of WeakMap, where a cache value
is associated with a weak object identifier, not the other way around.


Previous Comments:
------------------------------------------------------------------------
[2021-04-12 16:11:19] alex dot a dot pott at gmail dot com

Description:
------------
In the WeakReference documentation it says: "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."

And in the RFC it says:"The application might want to fetch the same serial number multiple
times. Fetching the same product again and making a new object could have several problems. It might
be slow and expensive."

However the current implementation means thats weak references don't prevent destruction when
an object loses scope. This means that a cache service that persists objects in memory can't
use the WeakReference object as hoped.

Ideally WeakReferences would prevent an object being destroyed until garbage collection is run.

For more information see https://www.drupal.org/project/drupal/issues/3190992#comment-14055154

Test script:
---------------
$obj = new stdClass;
$weakref = WeakReference::create($obj);
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
gc_collect_cycles();
var_dump($weakref->get());

Expected result:
----------------
object(stdClass)#1 (0) {
}
object(stdClass)#1 (0) {
}
NULL

Actual result:
--------------
object(stdClass)#1 (0) {
}
NULL
NULL


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80951&edit=1


Thread (3 messages)

« previous php.bugs (#233373) next »