PHPackages                             rs-world/instantiator - 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. rs-world/instantiator

ActiveLibrary

rs-world/instantiator
=====================

A dependency handler for php to provide loosely coupled design

v0.1.0(5y ago)12711MITPHPPHP ^7.1 || ^8.0

Since Nov 17Pushed 5y ago1 watchersCompare

[ Source](https://github.com/rs-world/instantiator)[ Packagist](https://packagist.org/packages/rs-world/instantiator)[ RSS](/packages/rs-world-instantiator/feed)WikiDiscussions main Synced 6d ago

READMEChangelogDependencies (1)Versions (2)Used By (1)

[![Build Status](https://camo.githubusercontent.com/990798cf915331be5094b8ff1efb1bde62b1dbdd3038f16bc001be7161441bdc/68747470733a2f2f696d672e736869656c64732e696f2f636972636c6563692f6275696c642f67682f72732d776f726c642f696e7374616e746961746f722f6d61696e3f7374796c653d666c61742d737175617265)](https://circleci.com/gh/rs-world/instantiator/tree/master)[![Issues](https://camo.githubusercontent.com/2f67f0f547eb0effe7608b077dcd5cfba8c4f615f11e72939c67f67945c825be/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f72732d776f726c642f696e7374616e746961746f723f7374796c653d666c61742d73717561726526636f6c6f723d626c7565)](https://github.com/rs-world/instantiator/issues)[![Forks](https://camo.githubusercontent.com/9824789882305ca7957ddb62be957a578ab4766e75082a8a748ad5d845d16808/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f72732d776f726c642f696e7374616e746961746f723f7374796c653d666c61742d73717561726526636f6c6f723d707572706c65)](https://github.com/rs-world/instantiator/network/members)[![Stars](https://camo.githubusercontent.com/c85943a2043520d8932b4c5b92ecf2fd46a86d28d9a79d47285e7440165a5bd7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f72732d776f726c642f696e7374616e746961746f723f7374796c653d666c61742d737175617265)](https://github.com/rs-world/instantiator/stargazers)[![License](https://camo.githubusercontent.com/dd86b79aebe144caed1764ea282c62f619b10319c37fbfa390fae7603037bdfe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f72732d776f726c642f696e7374616e746961746f723f636f6c6f723d7465616c267374796c653d666c61742d737175617265)](https://github.com/rs-world/instantiator/blob/master/LICENSE)

Instantiator
============

[](#instantiator)

Instantiator a very light weight small library which implements Instantiator pattern to solve dependency problems between software components or classes. For more about Instantiator pattern, visit [this link](https://github.com/reyadussalahin/instantiator-pattern).

Getting started
---------------

[](#getting-started)

Instantiator is very easy to use and handle dependencies. One of the great properties about is it is very flexible.

### Get Instantiator using composer

[](#get-instantiator-using-composer)

`Instantiator` library is not added to packgist yet. So, you need to add this repo to your composer and install it.You can add the following to your composer.json file:

```
{
    "repositories": [
        {
            "url": "https://github.com/rs-world/instantiator.git",
            "type": "git",
            "no-api": true
        }
    ],
    "require": {
        "rs-world/instantiator": "*"
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
```

Okay, once installed you can use it on your project.

### How to use Instantiator

[](#how-to-use-instantiator)

To use instantiator you must extends `Instantiator class`. Look at the example below which creates an `Instantiator` for `Database Class`:

```
class DatabaseIntantiator extends Instantiator
{
    protected function register()
    {
        $this->instance([
            "default" => function($a, $b) {
                return new \Path\To\Database($a, $b);
            },
            "test" => function($a, $b) {
                return new \Path\To\FakeDatabase($a, $b);
            }
        ]);
    }

    public function get(A $a, B $b): \Path\To\DatabaseInterface
    {
        return $this->getInstance($a, $b);
    }
}
```

Now, you can use DatabaseInstantiator class as follows:

```
// in default mode
$dbi = new DatabaseInstantiator();
$db = $dbi->get($a, $b);
var_dump($db instanceof \Path\To\Database); // prints true

// in test mode
$dbi = new DatabaseInstantiator("test", true);
$db = $dbi->get($a, $b);
var_dump($db instanceof \Path\To\DatabaseFake); // prints true
```

You can set `Instantiator`'s mode to `test` globally. To do that:

```
// set global mode to "test"
Instantiator::setGlobalMode("test");
// now
$dbi = new DatabaseInstantiator();
$db = $dbi->get($a, $b);
var_dump($db instanceof \Path\To\Database); // prints false
var_dump($db instanceof \Path\To\DatabaseFake); // prints true
```

### Resolving dependencies using Instantiator

[](#resolving-dependencies-using-instantiator)

You can easily handle dependency using Instantiator. Let there be a Service class which depends on Database. Then,

```
class ServiceInstantiator extends Instantator
{
    protected function register()
    {
        // ...
        // ...
        $dbi = new \Path\To\DatabaseInstantiator(
            $this->getMode(),
            $this->getFallback()
        );
        $db = $dbi->getInstance($a, $b);

        $this->instance([
            "default" => function($x) use($db) {
                return \Path\To\Service($db, $x);
            }
        ]);
    }

    public function get($x): ServiceInterface
    {
        return $this->getInstance($x);
    }
}
```

You can use it as follows:

```
// ...
$si = new ServiceInstantiator();
$service = $si->get($x);
// ...
```

And you don't have to worry about database instantiating, and also you don't have to worry about which databse class to use when it is testing time, cause "DatabaseFake" will be instantiating when mode is set to "test". It makes your task that simple!

Instantiator brings much more functionality to table, whether you're in production or testing or developing applicatons. The documentation of Instantiator is under process, and any type of contribution is very much welcome.

LICENSE
-------

[](#license)

To learn about the project license, visit [here](https://github.com/rs-world/instantiator/blob/master/LICENSE).

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

[](#contributing)

The project is ongoing and it has a lot of potential to grow. So, if you've any ideas or improvements, send a pull request. I'll have a look as soon as possible and provide feedback.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity48

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

Unknown

Total

1

Last Release

2006d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/45eeebeb6f4abac794505f14c85bfbb32f86e92d33db0796d6e0347a42aa2e75?d=identicon)[reyad](/maintainers/reyad)

---

Top Contributors

[![reyadussalahin](https://avatars.githubusercontent.com/u/31490329?v=4)](https://github.com/reyadussalahin "reyadussalahin (15 commits)")

---

Tags

autowiringdependency-handlerdependency-injectioninstantiatorinstantiator-patternconstructorinstantiatelibrarydependency-handler

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rs-world-instantiator/health.svg)

```
[![Health](https://phpackages.com/badges/rs-world-instantiator/health.svg)](https://phpackages.com/packages/rs-world-instantiator)
```

###  Alternatives

[doctrine/instantiator

A small, lightweight utility to instantiate objects in PHP without invoking their constructors

11.0k853.3M335](/packages/doctrine-instantiator)[filp/whoops

php error handling for cool kids

13.2k402.4M1.4k](/packages/filp-whoops)[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[symfony/var-exporter

Provides tools to export, instantiate, hydrate, clone and lazy-load PHP objects

2.1k378.1M441](/packages/symfony-var-exporter)[league/iso3166

ISO 3166-1 PHP Library

69536.3M116](/packages/league-iso3166)[dekor/php-array-table

PHP Library for printing associative arrays as text table (similar to mysql terminal console)

296.6M2](/packages/dekor-php-array-table)

PHPackages © 2026

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