定制 ocramius/lazy-map 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ocramius/lazy-map

最新稳定版本:2.8.0

Composer 安装命令:

composer require ocramius/lazy-map

包简介

A library that provides lazy instantiation logic for a map of objects

README 文档

README

This small library aims at providing a very simple and efficient map of lazy-instantiating objects.

Total Downloads Latest Stable Version Latest Unstable Version Mutation testing badge

Abandoned

Starting with PHP 8.3, dynamic properties are no longer allowed "out of the box". While it is still possible to have dynamic properties via explicit declaration (the #[\AllowDynamicProperties] attribute), the approach of this package is no longer to be considered safe nor efficient long-term.

Based on that, this package is deprecated and abandoned: please use traditional PHP arrays instead.

Installation

The suggested installation method is via composer:

composer require ocramius/lazy-map

Usage

The current implementation is very simple and allows to define a map of "services" through a LazyMap\CallbackLazyMap:

$map = new \LazyMap\CallbackLazyMap(function ($name) { $object = new \stdClass(); $object->name = $name; return $object; }); var_dump($map->foo); var_dump($map->bar); var_dump($map->{'something special'});

Purpose

The idea behind the library is to avoid un-efficient lazy-loading operations like following:

private function getSomething($name) { if (isset($this->initialized[$name]) || array_key_exists($name, $this->initialized)) { return $this->initialized[$name]; } return $this->initialized[$name] = new Something($name); }

This reduces overhead greatly when you'd otherwise call getSomething() thousands of times. That's especially useful when mapping a lot of different services and iterating over them over and over again.

Performance

LazyMap actually performs much better than the "un-efficient" example that I've shown above. You can look directly at the performance test suite for details on the tested implementations, but here are some results for you to have an idea of the boost:

Initialized Map Performance:

Method Name Ops/s Relative
initializedArrayPerformance 2,277,272.90002 100.00%
initializedArrayMapPerformance 1,536,988.76108 148.16%
initializedLazyMapPerformance 4,446,227.23514 51.22%

Un-Initialized Map Performance:

Method Name Ops/s Relative
unInitializedArrayPerformance : 1,091,720.80627 100.00%
unInitializedArrayMapPerformance 688,132.30083 158.65%
unInitializedLazyMapPerformance: 912,191.90744 119.68%

ocramius/lazy-map 适用场景与选型建议

ocramius/lazy-map 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 481.32k 次下载、GitHub Stars 达 87, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 ocramius/lazy-map 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 481.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 88
  • 点击次数: 29
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 87
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04