定制 neontsun/lazy-object 二次开发

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

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

neontsun/lazy-object

Composer 安装命令:

composer require neontsun/lazy-object

包简介

Wrapper for native lazy object in php 8.4

README 文档

README

Latest Stable Version PHP Version Require License Total Downloads Latest Unstable Version

Wrapper package over native lazy object functionality in PHP

Installation

You can add this library as a local, per-project dependency to your project using Composer:

composer require neontsun/lazy-object

If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:

composer require --dev neontsun/lazy-object

Usage

Add attribute to constructor fields that should be lazy loaded. If a constructor field is not marked with the lazy loading attribute, it will be considered a non-lazy field, the value of which must be passed through the property builder method.

final readonly class User
{
    public function __construct(
        private(set) string $id,
        #[Lazy]
        private(set) string $name,
        #[Lazy]
        private(set) int $age,
        #[Lazy]
        private(set) string $birthday,
    ) {}
}

There are two ways to create a lazy object - through a factory and through the implementation of the lazy loading interface by a class. In the first option, the phpstorm and static analyzers will not know what type was returned after creation, but this is solved by narrowing the type through instanceof, in the second option, the type of the created object will be available.

With factory

use Neontsun\LazyObject\Attribute\Lazy;

final readonly class Test 
{
    public function __construct(
        private(set) string $uuid,
        #[Lazy]
        private(set) array $data,
    ) {}
}
use Neontsun\LazyObject\LazyObjectFactory;
use Neontsun\LazyObject\DTO\Property;

$factory = new LazyObjectFactory();

$ghost = $factory
    ->ghost(Test::class)
    ->property('uuid', 'uuid')
    ->initializer(static function (): iterable {
        yield new Property(
            name: 'data',
            value: [1, 2, 3],
        );
    })
    ->build();

var_dump(new ReflectionClass(Test::class)->isUninitializedLazyObject($ghost));
var_dump($ghost);

The code above yields the output below:

bool(true)

lazy ghost object(Test)#402 (1) {
    ["uuid"]=>
    string(4) "uuid"
    ["data"]=>
    uninitialized(array)
}

With interface

use Neontsun\LazyObject\Attribute\Lazy;
use Neontsun\LazyObject\Contract\Builder\LazyGhostBuilderInterface;
use Neontsun\LazyObject\Contract\LazyInterface;
use Neontsun\LazyObject\Contract\LazyObjectFactoryInterface;
use Override;
use ReflectionClass;

final readonly class Test implements LazyInterface
{
    public function __construct(
        private(set) string $uuid,
        #[Lazy]
        private(set) array $data,
    ) {}
    
    #[Override]
    public static function lazy(LazyObjectFactoryInterface $factory): LazyGhostBuilderInterface
    {
        return $factory->ghost(self::class);
    }
    
    #[Override]
    public function isUninitialized(): bool
    {
        return new ReflectionClass(self::class)->isUninitializedLazyObject($this);
    }
}
use Neontsun\LazyObject\LazyObjectFactory;
use Neontsun\LazyObject\DTO\Property;

$factory = new LazyObjectFactory();

$ghost = Test::lazy($factory)
    ->property('uuid', 'uuid')
    ->initializer(static function (): iterable {
        yield new Property(
            name: 'data',
            value: [1, 2, 3],  
        ); 
    })
    ->build();

// $ghost is Test class for phpstrom 

var_dump($ghost->isUninitializes());
var_dump($ghost);

The code above yields the output below:

bool(true)

lazy ghost object(Test)#402 (1) {
    ["uuid"]=>
    string(4) "uuid"
    ["data"]=>
    uninitialized(array)
}

Real life case use

use Neontsun\LazyObject\Attribute\Lazy;
use Neontsun\LazyObject\Contract\LazyInterface;

final readonly class Task 
{
    public function __construct(
        private(set) string $title,
        private(set) string $description,
    ) {}
}

final readonly class TaskCollection implements LazyInterface
{
    /**
     * @param list<Task> $items
     */
    public function __construct(
        #[Lazy]
        private(set) array $items,
    ) {}
    
    #[Override]
    public static function lazy(LazyObjectFactoryInterface $factory): LazyGhostBuilderInterface
    {
        return $factory->ghost(self::class);
    }

    #[Override]
    public function isUninitialized(): bool
    {
        return new ReflectionClass(self::class)->isUninitializedLazyObject($this);
    }
    
    // methods...
}

final readonly class UserAggregate implements LazyInterface
{
    public function __construct(
        private(set) string $id,
        #[Lazy]
        private(set) string $name,
        #[Lazy]
        private(set) int $age,
        #[Lazy]
        private(set) string $createdAt,
        private(set) TaskCollection $tasks,
    ) {}
    
    #[Override]
    public static function lazy(LazyObjectFactoryInterface $factory): LazyGhostBuilderInterface
    {
        return $factory->ghost(self::class);
    }

    #[Override]
    public function isUninitialized(): bool
    {
        return new ReflectionClass(self::class)->isUninitializedLazyObject($this);
    }
    
    // methods...
}
use Neontsun\LazyObject\LazyObjectFactory;
use Neontsun\LazyObject\DTO\Property;

$factory = new LazyObjectFactory();

$tasksCollection = TaskCollection::lazy($factory)
    ->initializer(static function (): iterable {
        yield new Property(
            name: 'items',
            value: [
                new Task(
                    title: 'Title',
                    description: 'Description',
                ),
                // ...
            ],  
        );
    })
    ->build();

$userAggregate = UserAggregate::lazy($factory)
    ->property('id', 'uuid')
    ->property('tasks', $tasksCollection)
    ->initializer(static function (): iterable {
        yield from [
            new Property(
                name: 'name',
                value: 'Name', 
            ),
            new Property(
                name: 'age',
                value: 25, 
            ),
            new Property(
                name: 'createdAt',
                value: '2025-01-01 12:00:00', 
            ),
        ];
    })
    ->build();

var_dump($userAggregate);
var_dump($userAggregate->isUninitialized());
var_dump($userAggregate->name);
var_dump($userAggregate);
var_dump($userAggregate->isUninitialized());
var_dump($userAggregate->tasks);
var_dump($userAggregate->tasks->isUninitialized());
var_dump($userAggregate->tasks->items);
var_dump($userAggregate->tasks);
var_dump($userAggregate->tasks->isUninitialized());

The code above yields the output below:

lazy ghost object(UserAggregate)#383 (2) {
  ["id"]=>
  string(4) "uuid"
  ["name"]=>
  uninitialized(string)
  ["age"]=>
  uninitialized(int)
  ["createdAt"]=>
  uninitialized(string)
  ["tasks"]=>
  lazy ghost object(TaskCollection)#392 (0) {
    ["items"]=>
    uninitialized(array)
  }
}

bool(true)

string(4) "Name"

object(UserAggregate)#383 (5) {
  ["id"]=>
  string(4) "uuid"
  ["name"]=>
  string(4) "Name"
  ["age"]=>
  int(25)
  ["createdAt"]=>
  string(19) "2025-01-01 12:00:00"
  ["tasks"]=>
  lazy ghost object(TaskCollection)#392 (0) {
    ["items"]=>
    uninitialized(array)
  }
}

bool(false)

lazy ghost object(TaskCollection)#392 (0) {
  ["items"]=>
  uninitialized(array)
}

bool(true)

array(1) {
  [0]=>
  object(Task)#423 (2) {
    ["title"]=>
    string(5) "Title"
    ["description"]=>
    string(11) "Description"
  }
}

object(TaskCollection)#392 (1) {
  ["items"]=>
  array(1) {
    [0]=>
    object(Task)#423 (2) {
      ["title"]=>
      string(5) "Title"
      ["description"]=>
      string(11) "Description"
    }
  }
}

bool(false)

neontsun/lazy-object 适用场景与选型建议

neontsun/lazy-object 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.08k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-25