innmind/reflection

Library to build objects and extract data out of them

5.2.0 2023-09-16 16:38 UTC

This package is auto-updated.

Last update: 2024-12-16 19:09:36 UTC


README

Build Status codecov Type Coverage

Library to build objects and extract data out of them.

Build and inject data into an object

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>

This code will create a new Foo object and assign the property foo to 42 and bar to 'baz'.

Extracting data out of an object

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