Instantiate a class from an array of named parameters.
$ composer require keven/instantiator
<?php
use Keven\Instantiator\Instantiator;
class User
{
public function __construct(string $emailAddress, string $password, string $userName = null)
{
// ...
}
}
$user = (new Instantiator)->instantiate(
User::class,
[
'emailAddress' => '[email protected]',
'password' => 'CorrectHorseBatteryStaple',
]
);
You can also partially apply arguments:
<?php
// ...
$userCreator = (new Instantiator)->partial(
User::class,
[
'emailAddress' => '[email protected]',
]
);
$user = $userCreator(['password' => 'Tr0ub4dor&3']);