https://github.com/tysonandre/pecl-weakreference_bc
Polyfill of WeakMap/WeakReference for older PHP versions. This is a fork of the abandoned "weakref" PECL (https://github.com/colder/php-weakref)
https://github.com/tysonandre/pecl-weakreference_bc
php weakmap weakreference
Last synced: about 2 months ago
JSON representation
Polyfill of WeakMap/WeakReference for older PHP versions. This is a fork of the abandoned "weakref" PECL (https://github.com/colder/php-weakref)
- Host: GitHub
- URL: https://github.com/tysonandre/pecl-weakreference_bc
- Owner: TysonAndre
- License: other
- Created: 2021-12-04T18:56:57.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-12T01:48:43.000Z (over 2 years ago)
- Last Synced: 2025-02-11T09:18:41.113Z (3 months ago)
- Topics: php, weakmap, weakreference
- Language: C
- Homepage:
- Size: 165 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# The weakreference_bc PECL extension
[](https://github.com/TysonAndre/pecl-weakreference_bc/actions/workflows/main.yml?query=branch%3Amaster)
[](https://ci.appveyor.com/project/TysonAndre/pecl-weakreference-bc)**[weakreference_bc](https://github.com/TysonAndre/pecl-weakreference_bc) is a fork of the unmaintained https://pecl.php.net/weakref PECL. The `weakreference_bc` PECL changes the API to provide polyfills for [WeakReference](https://www.php.net/manual/en/class.weakreference.php)/[WeakMap](https://www.php.net/manual/en/class.weakmap.php)**
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.## WeakReference
The WeakReference class is a simple class that allows to access its referenced object
as long as it exists. Unlike other references, having this WeakReference object will
not prevent the object to be collected.See https://www.php.net/manual/en/class.weakreference.php
```php
get())) { // It does have a reference
echo "Object still exists!\n";
var_dump($r1->get());
} else {
echo "Object is dead!\n";
}unset($o1);
if (is_object($r1->get())) { // It doesn't have a reference
echo "Object still exists!\n";
var_dump($r1->get());
} else {
echo "Object is dead!\n";
}
?>
```## WeakMap
The WeakMap class is very similar to WeakReference, only that it also allows to
associate data to each object. When the target object gets destroyed, the
associated data is automatically freed.This is similar to https://www.php.net/manual/en/class.weakmap.php
(but currently implements Iterator instead of IteratorAggregate)```php
```
Iterating over WeakMap provides access to both the key (the reference)
and the value:```php
$v) {
// $k == $wmk
// $v == $wmv
}
?>
```## Limitations
Prior to php 7.0.3, using an object with `WeakReference::create` or as a key in an entry of of `WeakMap` would cause this to change the value of `spl_object_hash` and `SplObjectStorage::getHash`.
## Alternatives
A userland polyfill for WeakMap for php 7.4+ is https://github.com/BenMorel/weakmap-polyfill . That library offers a compatible implementation with the native WeakMap, but:
- slower
- whose values are not removed as soon as the object key is destroyed, but when you use the WeakMap again (eventually triggering housekeeping); note that this affects when object destructors are called as well