PHPackages                             code-distortion/di-caller - 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. code-distortion/di-caller

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

code-distortion/di-caller
=========================

A PHP package that calls callables/callbacks/hooks, using dependency injection to resolve their parameters

0.3.1(5mo ago)07.3k—0%2MITPHPPHP 7.0.\* | 7.1.\* | 7.2.\* | 7.3.\* | 7.4.\* | 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\* | 8.4.\* | 8.5.\*CI passing

Since Sep 11Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/code-distortion/di-caller)[ Packagist](https://packagist.org/packages/code-distortion/di-caller)[ Docs](https://github.com/code-distortion/di-caller)[ RSS](/packages/code-distortion-di-caller/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (6)Versions (8)Used By (2)

DI Caller
=========

[](#di-caller)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e5c4733421490fe6a96800f9fc6f1d40fd46fe0c4f5299861a9537b5e2992c2b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f64652d646973746f7274696f6e2f64692d63616c6c65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/code-distortion/di-caller)[![PHP Version](https://camo.githubusercontent.com/7377a12d33082dcc66e398bcdfe01550146ac043eef13190ae9c5c99018ee3a0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e30253230746f253230382e352d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/7377a12d33082dcc66e398bcdfe01550146ac043eef13190ae9c5c99018ee3a0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e30253230746f253230382e352d626c75653f7374796c653d666c61742d737175617265)[![GitHub Workflow Status](https://camo.githubusercontent.com/48319ae3398d83c5d97f8dd522dc1a0a7053aa18e0188ae4a56246100b206061/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f64652d646973746f7274696f6e2f64692d63616c6c65722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/code-distortion/di-caller/actions)[![Buy The World a Tree](https://camo.githubusercontent.com/dc3f77a9b22c3bc83c7b7d863bf138a7ca3418f1826b0b16d073d0aa87c16bc4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74726565776172652d2546302539462538432542332d6c69676874677265656e3f7374796c653d666c61742d737175617265)](https://plant.treeware.earth/code-distortion/di-caller)[![Contributor Covenant](https://camo.githubusercontent.com/902d296a65b2997bada7e7717fd929d9177f3bd95414cbb5ea2ed843c680f314/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6e7472696275746f72253230636f76656e616e742d76322e3125323061646f707465642d6666363962342e7376673f7374796c653d666c61742d737175617265)](.github/CODE_OF_CONDUCT.md)

***code-distortion/di-caller*** is a PHP package that instantiates classes, and calls callables / callbacks / hooks using dependency injection to resolve their parameters.

I built this to use in my own packages, where the caller wants to instantiate classes or pass callbacks, and the paramaters they actually need aren't know ahead of time. This package lets you specify the parameters you want to provide, and it resolves which ones are needed at call-time.

It isn't a Dependency Injection Container like those used in frameworks. Each callable is dealt with individually.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
    - [Instantiating Classes](#instantiating-classes)
    - [Running Callables](#running-callables)
- [Registering Parameters](#registering-parameters)
    - [Registering Parameters by Type](#registering-parameters-by-type)
    - [Registering Parameters by Name](#registering-parameters-by-name)
    - [Registering Parameters by Position](#registering-parameters-by-position)
    - [Union, Intersection and Variadic Types](#union-intersection-and-variadic-types)
- [Exceptions](#exceptions)
- [Error Checking](#error-checking)
    - [Checking When Instantiating Classes](#checking-when-instantiating-classes)
    - [Checking When Running Callables](#checking-when-running-callables)

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

[](#installation)

Install the package via composer:

```
composer require code-distortion/di-caller
```

Usage
-----

[](#usage)

There are three steps to using this package:

- Create a `DICaller` instance and pass the *class* you'd like to instantiate or *callable* you'd like to call,
- Register the parameters you'd like to make available,
- Instantiate the class using `->instantiate()` or execute the callable using `->call()`.

`DICaller` will match the parameters you've registered to the constructor or callable signature.

### Instantiating Classes

[](#instantiating-classes)

Instantiate classes by passing the *class* FQCN to `DICaller`, and calling `->instantiate()`:

```
namespace MyApp;

class User
{
    public function __construct(
        private string $firstName,
        private string $lastName,
    ) {}
}
```

```
use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$result = DICaller::new(User::class)
    ->registerByName('firstName', 'Bob')
    ->registerByName('lastName', 'Smith')
    ->instantiate(); // new User instance
```

### Running Callables

[](#running-callables)

Call a *callable* by passing it to `DICaller`, and running `->call()`:

```
use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$callable = fn(User $user) => "hello {$user->firstName}";

$result = DICaller::new($callable)
    ->registerType(new User('Bob', 'Smith'))
    ->call(); // 'hello Bob'
```

Registering Parameters
----------------------

[](#registering-parameters)

### Registering Parameters by Type

[](#registering-parameters-by-type)

You can register parameters by **type**, which supports class-type and variable type (integer, string, etc).

In this example, more parameters are registered than are actually needed. Only the necessary ones are used.

```
use CodeDistortion\DICaller\DICaller;
use MyApp\Request;
use MyApp\ShoppingCart;
use MyApp\User;

$callable = fn(Request $request, User $user, float $duration)
    => "$user->name ({$request->getIp()}) - $duration seconds";

$user = new User('Bob', 'Smith');
$request = new Request(…);
$shoppingCart = new ShoppingCart(…);
$someId = 10;
$duration = 4.55;

$result = DICaller::new($callable)
    ->registerByType($user)         // registerByType($user, User::class)                 // registerByName('param1', 'hello') // registerByPosition(0, 'hello') // instantiate()` is called:

```
use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$user = DICaller::new(User::class)->instantiate();
// throws DICallerInstantiationException - when there are unresolved parameters
```

If a callable can't be called, a `DICallerCallableException` is thrown when `->call()` is run:

```
use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)->call(); // throws DICallerCallableException
```

Error Checking
--------------

[](#error-checking)

### Checking When Instantiating Classes

[](#checking-when-instantiating-classes)

Before calling `->instantiate()`, you can check to see if the class can *actually* be instantiated using `->canInstantiate()`.

If the parameters satisfy the needs of the constructor, `->canInstantiate()` will return `true`. Otherwise, it will return `false`.

```
use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$instantiator = DICaller::new(User::class)
    ->registerByName('middleName', 'John');
$user = $instantiator->canInstantiate() // false - because $firstName and $lastName are unresolved
    ? $instantiator->instantiate()
    : null;
```

And you can check &amp; instantiate in one step using `->instantiateIfPossible()`:

```
use CodeDistortion\DICaller\DICaller;
use MyApp\User;

$user = DICaller::new(User::class)
    ->registerByName('middleName', 'John')
    ->instantiateIfPossible();
// null - because $firstName and $lastName are unresolved
```

This will return the object, or `null` if it can't be instantiated.

### Checking When Running Callables

[](#checking-when-running-callables)

Before calling `->call()`, you can check that the callable is *actually callable*, and the parameters resolve properly using `->canCall()`:

```
use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$caller = DICaller::new($callable)
    ->registerByName('param3', 'something');
if ($caller->canCall()) { // false - because $param1 and $param2 are unresolved
    $result = $caller->call();
}
```

And you can check &amp; call it in one step using `->callIfPossible()`:

```
use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)
    ->registerByName('param3', 'something')
    ->callIfPossible(); // null - because $param1 and $param2 are unresolved
```

> ***Note:*** This will return `null` when the call isn't possible, which isn't distinguishable from a successful call that returns `null` on purpose.

Testing This Package
--------------------

[](#testing-this-package)

- Clone this package: `git clone https://github.com/code-distortion/di-caller.git .`
- Run `composer install` to install dependencies
- Run the tests: `composer test`

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

### SemVer

[](#semver)

This library uses [SemVer 2.0.0](https://semver.org/) versioning. This means that changes to `X` indicate a breaking change: `0.0.X`, `0.X.y`, `X.y.z`. When this library changes to version 1.0.0, 2.0.0 and so forth, it doesn't indicate that it's necessarily a notable release, it simply indicates that the changes were breaking.

Treeware
--------

[](#treeware)

This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/code-distortion/di-caller) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

### Code of Conduct

[](#code-of-conduct)

Please see [CODE\_OF\_CONDUCT](.github/CODE_OF_CONDUCT.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Tim Chandler](https://github.com/code-distortion)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance70

Regular maintenance activity

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~73 days

Recently: every ~85 days

Total

7

Last Release

176d ago

PHP version history (4 changes)0.1.0PHP 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\*

0.2.1PHP 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\* | 8.4.\*

0.2.3PHP 7.0.\* | 7.1.\* | 7.2.\* | 7.3.\* | 7.4.\* | 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\* | 8.4.\*

0.3.1PHP 7.0.\* | 7.1.\* | 7.2.\* | 7.3.\* | 7.4.\* | 8.0.\* | 8.1.\* | 8.2.\* | 8.3.\* | 8.4.\* | 8.5.\*

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/56794290?v=4)[Tim](/maintainers/code-distortion)[@code-distortion](https://github.com/code-distortion)

---

Top Contributors

[![code-distortion](https://avatars.githubusercontent.com/u/56794290?v=4)](https://github.com/code-distortion "code-distortion (26 commits)")

---

Tags

dependencyinjectioncallabledicallbackparameterresolverHOOKargumentscaller

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/code-distortion-di-caller/health.svg)

```
[![Health](https://phpackages.com/badges/code-distortion-di-caller/health.svg)](https://phpackages.com/packages/code-distortion-di-caller)
```

###  Alternatives

[php-di/invoker

Generic and extensible callable invoker

26857.8M56](/packages/php-di-invoker)[rybakit/arguments-resolver

ArgumentsResolver allows you to determine the arguments to pass to a function or method.

26107.7k7](/packages/rybakit-arguments-resolver)[zenstruck/callback

Callable wrapper to validate and inject arguments.

569.6M4](/packages/zenstruck-callback)[marcelog/ding

PHP Dependency Injection based on Spring(tm), with Aspect Oriented Programming, MVC

1202.1k](/packages/marcelog-ding)[rochamarcelo/cake-pimple-di

A cakephp plugin for dependency injection based on Pimple library

12176.8k](/packages/rochamarcelo-cake-pimple-di)[phpfluent/callback

Allows you execute callbacks in a more dynamic way

1159.2k1](/packages/phpfluent-callback)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
