ali-eltaweel/lazy-props
Composer 安装命令:
composer require ali-eltaweel/lazy-props
包简介
PHP Lazy-initialized properties
README 文档
README
Installation
Install lazy-props via Composer:
composer require ali-eltaweel/lazy-props
Usage
Declaring Lazy Properties
use Lang\{ Annotations\LazyInitialized, LazyProperties }; class Database { use LazyProperties; #[LazyInitialized('createConnection')] public $connection; function createConnection() { return new stdClass(); } }
Upon instantiation of the Database class, the connection property will be unset, so that calls to $db->connection are directed to the __get magic method provided by the LazyProperties trait. This method will then call the createConnection method on the first call to $db->connection, and set the connection property to the return value of that method. Subsequent calls to $db->connection will return the already initialized value.
Common Initializer Method
If your class declares more than one lazy property, you can use a common initializer method for all of them:
class X { use LazyProperties; #[LazyInitialized('createArgument')] public int $x; #[LazyInitialized('createArgument')] public int $y; private function createArgument(string $name): int { echo "Initializing {$name}...\n"; return match ($name) { 'x' => 42, 'y' => 84, }; } }
You can also you the InitializerMethod annotation on your class to save yourself some typing:
#[InitializerMethod('createArgument')] class X { #[LazyInitialized()] public int $x; #[LazyInitialized()] public int $y; }
Subclasses's Readonly, Lazy-initialized Properties
Take a look at the following example:
class X { use LazyProperties; #[LazyInitialized('getX')] public int $x; private function getX() { return 42; } } class Y extends X { #[LazyInitialized('getY')] public int $y; // you can't make this initializer private, because it is called from the parent class. protected function getY() { echo "Initializing y...\n"; return 84; } }
It looks fine, and it is...
$y = new Y(); var_dump($y->y); var_dump($y->y); // "Initializing y..." is printed only once
But, when you change the y property to be readonly:
class Y extends X { #[LazyInitialized('getY')] public readonly int $y; }
... you will get an error by just instantiating the Y class:
Cannot unset readonly property Y::$y from scope X
To fix this; each class has to unset (uninitialize) its own lazy properties, to do so, use the LazyPropertiesExtension trait in subclasses (of all generations) and annotate them with the ExtendedLazyProperties annotation giving the names of the extended lazy properties:
use Lang\{ Annotations\ExtendedLazyProperties, LazyPropertiesExtension }; #[ExtendedLazyProperties('y')] class Y extends X { use LazyPropertiesExtension; #[LazyInitialized('getY')] public readonly int $y; }
#[ExtendedLazyProperties('z')] class Z extends Y { use LazyPropertiesExtension; #[LazyInitialized('getZ')] public readonly int $z; }
ali-eltaweel/lazy-props 适用场景与选型建议
ali-eltaweel/lazy-props 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ali-eltaweel/lazy-props 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ali-eltaweel/lazy-props 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 91
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-06-20