Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Invoke Laravel Package

dantleech/invoke

Emulate named arguments in PHP: instantiate classes or call methods by mapping associative array keys to parameter names. Validates required/missing/extra keys and type hints, throwing clear exceptions. Useful for deserialization and config loading.

View on GitHub
Deep Wiki
Context7

Invoke

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

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle