PHPackages                             dantleech/invoke - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. dantleech/invoke

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dantleech/invoke
================

Emulate named parameters

2.0.0(5y ago)5730.1k↓23.2%2[1 PRs](https://github.com/dantleech/invoke/pulls)8MITPHPPHP ^7.2||^8.0

Since Dec 7Pushed 4y ago1 watchersCompare

[ Source](https://github.com/dantleech/invoke)[ Packagist](https://packagist.org/packages/dantleech/invoke)[ RSS](/packages/dantleech-invoke/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (7)Used By (8)

Invoke
======

[](#invoke)

[![Build Status](https://camo.githubusercontent.com/9550acae5016d024357dc530dfc7de2804965d4c283f736b2af4bc56250a04e1/68747470733a2f2f7472617669732d63692e6f72672f64616e746c656563682f696e766f6b652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dantleech/invoke)

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

PHP does not currently support [named parameters](https://wiki.php.net/rfc/named_params), this utility provides a convenient way to emulate them.

Installation
------------

[](#installation)

Require with composer:

```
$ composer require dantleech/invoke
```

Why
---

[](#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
-----------

[](#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
-----

[](#usage)

Instantiate a new class:

```
