innmind/reflection

用于构建对象并从中提取数据的库

5.2.0 2023-09-16 16:38 UTC

This package is auto-updated.

Last update: 2024-09-16 18:36:10 UTC


README

Build Status codecov Type Coverage

用于构建对象并从中提取数据的库。

构建对象并注入数据

use Innmind\Reflection\Instanciate;
use Innmind\Immutable\{
    Map,
    Maybe,
};

final class Foo
{
    private int $foo;
    private mixed $bar;

    public function __construct(string $foo)
    {
        $this->foo = $foo;
    }
}

$object = (new Instanciate)(Foo::class, Map::of(
    ['foo', 42],
    ['bar', 'baz'],
)); // Maybe<Foo>

此代码将创建一个新的 Foo 对象,并将属性 foo 赋值为 42,将 bar 赋值为 'baz'

从对象中提取数据

use Innmind\Reflection\Extract;
use Innmind\Immutable\{
    Set,
    Maybe,
    Map,
};

$properties = (new Extract)($myObject, Set::of('foo', 'bar', 'baz')); // Maybe<Map<non-empty-string, mixed>>