ocramius/lazy-property
最新稳定版本:2.5.0
Composer 安装命令:
composer require ocramius/lazy-property
包简介
A library that provides lazy instantiation logic for object properties
README 文档
README
This small library aims at providing a very simple and efficient loading of lazy properties
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-property
Use case
In many cases where lazy-initialization of private/protected properties is necessary, many people write classes that look like following:
class SomeService { protected $dependency; public function doWork() { $this->getDependency()->delegateWork(); } protected function getDependency() { return $this->dependency ?: $this->dependency = get_dependency_somehow(); } }
This is problematic because implementors and people subclassing SomeService will eventually write:
class SomethingElse extends SomeService { public function doOtherWork() { $this->dependency->doMoreWork(); } }
This can work only if SomeService#getDependency() was called at least once upfront (which may well be under certain circumstances), and therefore is a cause of bugs/headaches/suicides/etc.
In order to avoid this problem, the implementor of SomeService that is also exposing its protected $dependency property may just use LazyProperty\LazyPropertiesTrait to fix the problem:
#[\AllowDynamicProperties] class SomeService { use \LazyProperty\LazyPropertiesTrait; protected MyDependency $dependency; public function __construct() { $this->initLazyProperties(['dependency']); } public function doWork() { // look ma! no getter! $this->dependency->delegateWork(); } protected function getDependency() { return $this->dependency ?: $this->dependency = get_dependency_somehow(); } }
With this, any access to SomeService#$dependency will cause a call to SomeService#getDependency() if the property was not already initialized.
class SomethingElse extends SomeService { public function doOtherWork() { // always works $this->dependency->doMoreWork(); } }
Please note that a getter is required in order for the property to be lazy.
Performance notes
Using LazyProperty\LazyPropertiesTrait allows to speed up applications where a massive amount of getter calls is going on in private/protected scope. There is some minor overhead in calling SomeService#initLazyProperties(), as well as in the first property access, but it should be negligible.
ocramius/lazy-property 适用场景与选型建议
ocramius/lazy-property 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84.68k 次下载、GitHub Stars 达 69, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「utility」 「lazy」 「lazy loading」 「lazy instantiation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ocramius/lazy-property 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ocramius/lazy-property 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ocramius/lazy-property 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lazy loading for middleware and request handlers
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Laravel API Resources supercharged to get rid of N+1 problems.
Integrates data-mapper in Symfony projects.
Port of Facebook's dataloader to PHP
PACE (Progress Automatically Certain to Entertain) - Automatic page load progress bar for Yii PHP Framework
统计信息
- 总下载量: 84.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 69
- 点击次数: 25
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04