{"id":16915896,"url":"https://github.com/tysonandre/pecl-weakreference_bc","last_synced_at":"2026-05-01T14:32:19.438Z","repository":{"id":45130185,"uuid":"434984661","full_name":"TysonAndre/pecl-weakreference_bc","owner":"TysonAndre","description":"Polyfill of WeakMap/WeakReference for older PHP versions. This is a fork of the abandoned \"weakref\" PECL (https://github.com/colder/php-weakref)","archived":false,"fork":false,"pushed_at":"2022-10-12T01:48:43.000Z","size":169,"stargazers_count":1,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T13:33:47.665Z","etag":null,"topics":["php","weakmap","weakreference"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TysonAndre.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-04T18:56:57.000Z","updated_at":"2022-10-12T01:48:46.000Z","dependencies_parsed_at":"2023-01-19T21:17:56.837Z","dependency_job_id":null,"html_url":"https://github.com/TysonAndre/pecl-weakreference_bc","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/TysonAndre/pecl-weakreference_bc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TysonAndre%2Fpecl-weakreference_bc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TysonAndre%2Fpecl-weakreference_bc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TysonAndre%2Fpecl-weakreference_bc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TysonAndre%2Fpecl-weakreference_bc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TysonAndre","download_url":"https://codeload.github.com/TysonAndre/pecl-weakreference_bc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TysonAndre%2Fpecl-weakreference_bc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32501399,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","weakmap","weakreference"],"created_at":"2024-10-13T19:23:31.141Z","updated_at":"2026-05-01T14:32:19.421Z","avatar_url":"https://github.com/TysonAndre.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The weakreference_bc PECL extension\n\n[![Build Status](https://github.com/TysonAndre/pecl-weakreference_bc/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/TysonAndre/pecl-weakreference_bc/actions/workflows/main.yml?query=branch%3Amaster)\n[![Build Status (Windows)](https://ci.appveyor.com/api/projects/status/5duym2sdcy348e67?svg=true)](https://ci.appveyor.com/project/TysonAndre/pecl-weakreference-bc)\n\n\n**[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)**\n\nA weak reference provides a gateway to an object without preventing that object\nfrom being collected by the garbage collector (GC). It allows to associate\ninformation to volatile object. It is useful to associate metadata or cache\ninformation to objects. Indeed, the cache entry should not be preventing the\ngarbage collection of the object AND the cached info when the object is no\nlonger used.\n\n## WeakReference\nThe WeakReference class is a simple class that allows to access its referenced object\nas long as it exists. Unlike other references, having this WeakReference object will\nnot prevent the object to be collected.\n\nSee https://www.php.net/manual/en/class.weakreference.php\n\n```php\n\u003c?php\nclass MyClass {\n    public function __destruct() {\n        echo \"Destroying object!\\n\";\n    }\n}\n\n$o1 = new MyClass;\n\n$r1 = WeakReference::create($o1);\n\nif (is_object($r1-\u003eget())) { // It does have a reference\n    echo \"Object still exists!\\n\";\n    var_dump($r1-\u003eget());\n} else {\n    echo \"Object is dead!\\n\";\n}\n\nunset($o1);\n\nif (is_object($r1-\u003eget())) { // It doesn't have a reference\n    echo \"Object still exists!\\n\";\n    var_dump($r1-\u003eget());\n} else {\n    echo \"Object is dead!\\n\";\n}\n?\u003e\n```\n\n## WeakMap\nThe WeakMap class is very similar to WeakReference, only that it also allows to\nassociate data to each object. When the target object gets destroyed, the\nassociated data is automatically freed.\n\nThis is similar to https://www.php.net/manual/en/class.weakmap.php\n(but currently implements Iterator instead of IteratorAggregate)\n\n```php\n\u003c?php\n$wm = new WeakMap();\n\n$o = new StdClass;\n\nclass A {\n    public function __destruct() {\n        echo \"Dead!\\n\";\n    }\n}\n\n$wm[$o] = new A;\n\nvar_dump(count($wm)); // int(1)\necho \"Unsetting..\\n\";\nunset($o); // Will destroy the 'new A' object as well\necho \"Done\\n\";\nvar_dump(count($wm)); // int(0)\n?\u003e\n```\n\nIterating over WeakMap provides access to both the key (the reference)\nand the value:\n\n```php\n\u003c?php\n$wm = new WeakMap();\n\n$wmk = new StdClass;\n$wmv = new StdClass;\n\n$wm[$wmk] = $wmv;\n\nforeach($wm as $k =\u003e $v) {\n    // $k == $wmk\n    // $v == $wmv\n}\n?\u003e\n```\n\n## Limitations\n\nPrior 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`.\n\n## Alternatives\n\nA 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:\n\n- slower\n- 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\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftysonandre%2Fpecl-weakreference_bc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftysonandre%2Fpecl-weakreference_bc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftysonandre%2Fpecl-weakreference_bc/lists"}