Skip to content
PECL extension that implements weak references and weak maps in PHP
C PHP
Find file
Latest commit b82e7ce Jan 24, 2016 3 @colder Release Weakref 0.3.2
Failed to load latest commit information.
tests Release Weakref 0.3.2 Jan 24, 2016
.gdbinit
.gitignore ignore more files Jan 13, 2016
CREDITS Code for weakref pecl ext Aug 3, 2011
LICENSE
README.md
config.m4
config.w32
package.xml
php_weakref.c
php_weakref.h Release Weakref 0.3.2 Jan 24, 2016
wr_store.c
wr_store.h
wr_weakmap.c
wr_weakmap.h
wr_weakref.c
wr_weakref.h

README.md

The Weakref PECL extension

A weak reference provides a gateway to an object without preventing that object from being collected by the garbage collector (GC). It allows to associate information to volatile object. It is useful to associate metadata or cache information to objects. Indeed, the cache entry should not be preventing the garbage collection of the object AND the cached info when the object is no longer used.

More information about releases can be found on http://pecl.php.net/weakref

Weakref

The weakref class is a simple class that allows to access its referenced object as long as it exists. Unlike other references, having this Weakref object will not prevent the object to be collected.

<?php
class MyClass {
    public function __destruct() {
        echo "Destroying object!\n";
    }
}

$o1 = new MyClass;

$r1 = new Weakref($o1);

if ($r1->valid()) { // It does
    echo "Object still exists!\n";
    var_dump($r1->get());
} else {
    echo "Object is dead!\n";
}

unset($o1);

if ($r1->valid()) { // It doesn't
    echo "Object still exists!\n";
    var_dump($r1->get());
} else {
    echo "Object is dead!\n";
}
?>

Weakmap

The Weakmap class is very similar to Weakref, only that it also allows to associate data to each object. When the target object gets destroyed, the associated data is automatically freed.

<?php
$wm = new WeakMap();

$o = new StdClass;

class A {
    public function __destruct() {
        echo "Dead!\n";
    }
}

$wm[$o] = new A;

var_dump(count($wm)); // int(1)
echo "Unsetting..\n";
unset($o); // Will destroy the 'new A' object as well
echo "Done\n";
var_dump(count($wm)); // int(0)
?>

As of PHP7, iterating WeakMap provides access to both the key (the reference) and the value:

<?php
$wm = new WeakMap();

$wmk = new StdClass;
$wmv = new StdClass;

$wm[$wmk] = $wmv;

foreach($wm as $k => $v) {
    // $k == $wmk
    // $v == $wmv
}
?>
Something went wrong with that request. Please try again.