Skip to content

kevengodet/instantiator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

keven/instantiator

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

About

Instantiate an object based on an array of its constructor parameters

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages