PHP code example of neontsun / lazy-object

1. Go to this page and download the library: Download neontsun/lazy-object library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

neontsun / lazy-object example snippets


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,
    ) {}
}

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);

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);

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());