keven/instantiator

Instantiate an object based on an array of its constructor parameters.

1.1.0 2018-03-06 10:30 UTC

This package is auto-updated.

Last update: 2024-04-23 06:18:40 UTC


README

Instantiate a class from an array of named parameters.

Install

$ composer require keven/instantiator

Usage

<?php

use Keven\Instantiator\Instantiator;

class User
{
    public function __construct(string $emailAddress, string $password, string $userName = null)
    {
        // ...
    }
}

$user = (new Instantiator)->instantiate(
            User::class,
            [
                'emailAddress' => 'john@example.com',
                'password' => 'CorrectHorseBatteryStaple',
            ]
        );

You can also partially apply arguments:

<?php

// ...

$userCreator = (new Instantiator)->partial(
            User::class,
            [
                'emailAddress' => 'john@example.com',
            ]
        );

$user = $userCreator(['password' => 'Tr0ub4dor&3']);