keven/instantiator

根据构造函数参数数组实例化对象。

1.1.0 2018-03-06 10:30 UTC

This package is auto-updated.

Last update: 2024-09-23 07:10:03 UTC


README

从命名参数数组中实例化类。

安装

$ 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' => 'john@example.com',
                'password' => 'CorrectHorseBatteryStaple',
            ]
        );

您还可以部分应用参数

<?php

// ...

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

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