benmorel/weakmap-polyfill 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

benmorel/weakmap-polyfill

Composer 安装命令:

composer require benmorel/weakmap-polyfill

包简介

A WeakMap polyfill for PHP 7.4

README 文档

README

This polyfill aims to be 100% compatible with WeakMap in PHP 8.

Build Status Coverage Status Latest Stable Version Total Downloads License

Introduction

PHP 7.4 introduced WeakReference, but didn't include a WeakMap implementation. This has been implemented since, but is only available in PHP 8.

The RFC author, Nikita Popov, highlights why a userland WeakMap is suboptimal:

Weak maps require first-class language support and cannot be implemented using existing functionality provided by PHP.

At first sight, it may seem that an array mapping from spl_object_id() to arbitrary values could serve the purpose of a weak map. This is not the case for multiple reasons:

  • spl_object_id() values are reused after the object is destroyed. Two different objects can have the same object ID – just not at the same time.
  • The object ID cannot be converted back into an object, so iteration over the map is not possible.
  • The value stored under the ID will not be released when the object is destroyed.

Using the WeakReference class introduced in PHP 7.4, it is possible to avoid the first two issues (…). However, this does not solve the third problem: The data will not be released when the object is destroyed. It will only be released on the next access with an object that has the same reused ID, or if a garbage collection mechanism, which performs regular sweeps of the whole map, is implemented.

A native weak map implementation will instead remove the value from the weak map as soon as the object key is destroyed.

This is the trade-off this library offers: a 100% compatible implementation, but:

  • slower
  • whose values are not removed as soon as the object key is destroyed, but when you use the WeakMap again; note that this affects when object destructors are called as well

Here is how it works:

  • calls to count() will always garbage collect dangling references immediately
  • using array-like features: set, get, isset(), unset() will perform garbage collection at least every 100 operations (less often for large WeakMaps).
  • when GC cycles are collected (automatically or via gc_collect_cycles()), garbage collection will trigger immediately

This offers a reasonable trade-off between performance and memory usage.

Installation

This library is installable via Composer:

composer require benmorel/weakmap-polyfill

Requirements

This library requires PHP 7.4 or later.

Quickstart

$weakMap = new WeakMap();

$a = new stdClass();
$b = new stdClass();

$weakMap[$a] = 123;

var_export(isset($weakMap[$a])); // true
var_export(isset($weakMap[$b])); // false

echo $weakMap[$a]; // 123
echo $weakMap[$b]; // Error

echo count($weakMap); // 1

// removing the last reference to the object will remove it from the WeakMap
unset($a);

echo count($weakMap); // 0

Alternatives

The weakreference_bc PECL backports a native polyfill for WeakReference and WeakMap to PHP 7.0-7.4. The PECL has the following advantages:

  • weakreference_bc supports PHP 7.0+
  • Like the real WeakMap/WeakReference, values are removed as soon as the object key is destroyed.
  • Native implementations are faster and use less memory.
  • weakreference_bc's WeakMap::count() is fast because it does not need to perform garbage collection.

This has the following drawbacks:

  • Installing a PECL has more steps than installing a composer package.
  • weakreference_bc currently implements Iterator instead of the more accurate IteratorAggregate as of 0.4.1.

benmorel/weakmap-polyfill 适用场景与选型建议

benmorel/weakmap-polyfill 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.61M 次下载、GitHub Stars 达 47, 最近一次更新时间为 2020 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「weakref」 「weakreference」 「weakmap」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 benmorel/weakmap-polyfill 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 benmorel/weakmap-polyfill 我们能提供哪些服务?
定制开发 / 二次开发

基于 benmorel/weakmap-polyfill 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.61M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 47
  • 点击次数: 26
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 47
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-20