定制 dantleech/invoke 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dantleech/invoke

最新稳定版本:2.0.0

Composer 安装命令:

composer require dantleech/invoke

包简介

Emulate named parameters

README 文档

README

Build Status

Utility class to create new classes or invoke methods using named arguments.

PHP does not currently support named parameters, this utility provides a convenient way to emulate them.

Installation

Require with composer:

$ composer require dantleech/invoke

Why

Sometimes arguments may be sourced from arrays e.g. for "deserialization" or instantiating configuration nodes).

Validating the existence of array keys, checking their types etc. is error prone and time consuming.

By using Invoke::new(MyObject::class, $array) you can map the array keys directly to the __construct parameters.

This library will, throw descriptive exceptions:

  • If there are extra keys.
  • If there are missing required keys (i.e. non-nullable values).
  • If the types are wrong.

Performance

Inoke::new(Class::class, []) is around 50x slower than new Class(), or 260,000 operations per second vs. ~13,000,000.

+--------------------------+---------+
| subject                  | mode    |
+--------------------------+---------+
| benchInvokeNewClass      | 3.720μs |
| benchInstantiateNewClass | 0.076μs |
+--------------------------+---------+

Usage

Instantiate a new class:

<?php

use DTL\Invoke\Invoke;

class Foobar
{
    public function __construct(string $arg1, string $arg2 = 'val1')
    {
    }
}

$foo = Invoke::new(Foobar::class, [
    'arg1' => 'value1'
]);

Invoke a method:

<?php

use DTL\Invoke\Invoke;

class Foobar
{
    // ...

    public function one(string $two)
    {
    }
}

$foo = Invoke::new(Foobar::class, [
    'arg1' => 'value1'
]);

$bar= Invoke::method($foo, 'one', [
    'two' => 'bar'
]);

Alternatives

nikolaposa/cascader Utility for creating objects in PHP from constructor parameters definitions.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

统计信息

  • 总下载量: 661.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 6
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04