This library is based on php-weak PHP extension and provides various weak data structures:
php-weak PHP extension required. PHP 7 only (due to php-weak).
composer require pinepain/php-weak-lib
Mapping class that references keys weakly. Entries will be discarded when there is no longer a reference to the key.
This can be used to associate additional data with an object owned by other parts of an application without adding
attributes to those objects. This can be especially useful with objects that override attribute accesses.
Built on top of SplObjectStorage
.
Because a Weak\WeakKeyMap
is built on top of a SplObjectStorage
, it must not change size when
iterating over it. This can be difficult to ensure for a Weakref\WeakKeyMap
because actions performed by the program
during iteration may cause items in the storage to vanish "by magic" (as a side effect of garbage collection).
<?php
require __DIR__ . '/../vendor/autoload.php';
use Weak\WeakKeyMap;
$map = new WeakKeyMap();
$obj1 = new stdClass();
$inf1 = new stdClass();
$obj2 = new stdClass();
$inf2 = new stdClass();
$map->attach($obj1, $inf1);
$map->attach($obj2, $inf2);
var_dump($map->count()); // 2
// Let's destroy key
$obj1 = null;
var_dump($map->count()); // 1
Mapping class that references values weakly. Entries will be discarded when reference to the value exists any more.
Built on top of SplObjectStorage
.
Because a Weak\WeakValueMap
is built on top of a SplObjectStorage
, it must not change size when
iterating over it. This can be difficult to ensure for a Weakref\WeakValueMap
because actions performed by the program
during iteration may cause items in the storage to vanish "by magic" (as a side effect of garbage collection).
<?php
require __DIR__ . '/../vendor/autoload.php';
use Weak\WeakValueMap;
$map = new WeakValueMap();
$obj1 = new stdClass();
$inf1 = new stdClass();
$obj2 = new stdClass();
$inf2 = new stdClass();
$map->attach($obj1, $inf1);
$map->attach($obj2, $inf2);
var_dump($map->count()); // 2
// Let's destroy value
$inf1 = null;
var_dump($map->count()); // 1
Mapping class that references values weakly. Entries will be discarded when reference to the key or value exists any more.
Built on top of SplObjectStorage
.
Because a Weak\WeakKeyValueMap
is built on top of a SplObjectStorage
, it must not change size when
iterating over it. This can be difficult to ensure for a Weakref\WeakKeyValueMap
because actions performed by the program
during iteration may cause items in the storage to vanish "by magic" (as a side effect of garbage collection).
<?php
require __DIR__ . '/../vendor/autoload.php';
use Weak\WeakValueMap;
$map = new WeakKeyValueMap();
$obj1 = new stdClass();
$inf1 = new stdClass();
$obj2 = new stdClass();
$inf2 = new stdClass();
$map->attach($obj1, $inf1);
$map->attach($obj2, $inf2);
var_dump($map->count()); // 2
// Let's destroy key
$obj1 = null;
var_dump($map->count()); // 1
// Let's destroy value
$inf2 = null;
var_dump($map->count()); // 0
php-weak-lib PHP library is licensed under the MIT license.