PHPackages                             renakdup/simple-dic - 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. renakdup/simple-dic

ActiveLibrary

renakdup/simple-dic
===================

Simple DI Container with autowiring for your WordPress application with NO dependencies.

1.2.2(1y ago)101.8k1[10 issues](https://github.com/renakdup/simple-dic/issues)1MITPHPPHP ^7.4|^8CI passing

Since Nov 28Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/renakdup/simple-dic)[ Packagist](https://packagist.org/packages/renakdup/simple-dic)[ RSS](/packages/renakdup-simple-dic/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (18)Used By (1)

Simple DIC - WordPress DI Container in one file for your plugin/project.
========================================================================

[](#simple-dic---wordpress-di-container-in-one-file-for-your-pluginproject)

[![UnitTests](https://github.com/renakdup/simple-wordpress-dic/actions/workflows/phpunit.yaml/badge.svg)](https://github.com/renakdup/simple-wordpress-dic/actions/workflows/phpunit.yaml)[![PHPStan](https://github.com/renakdup/simple-wordpress-dic/actions/workflows/phpstan.yaml/badge.svg)](https://github.com/renakdup/simple-wordpress-dic/actions/workflows/phpstan.yaml)

[![Latest Stable Version](https://camo.githubusercontent.com/51a8154e74b5a6a854b14a6ed711f88997be067e53a1d70807916e4a34074f46/68747470733a2f2f706f7365722e707567782e6f72672f72656e616b6475702f73696d706c652d6469632f76)](https://packagist.org/packages/renakdup/simple-dic)[![PHP Version Require](https://camo.githubusercontent.com/610dfec9d88712530faabdfc3d54bec7ae004e70a406d71cb82e7794faaecbb2/68747470733a2f2f706f7365722e707567782e6f72672f72656e616b6475702f73696d706c652d6469632f726571756972652f706870)](https://packagist.org/packages/renakdup/simple-dic)[![Total Downloads](https://camo.githubusercontent.com/72be15f345c451d080065eb0011cfd870751f9f894655ba9dc37bd4f79e1fcfd/68747470733a2f2f706f7365722e707567782e6f72672f72656e616b6475702f73696d706c652d6469632f646f776e6c6f616473)](https://packagist.org/packages/renakdup/simple-dic)

WordPress DI Container with **autowiring** in a single file **with NO dependencies** allows you to easily use it in your PHP applications and especially convenient for **WordPress** plugins and themes.

Important

**100% code coverage**

Why choose Simple DI Container
------------------------------

[](#why-choose-simple-di-container)

1. Easy to integrate in your WordPress project - just copy one file or install via Composer.
2. No dependencies on other scripts or libraries.
3. Supports auto-wiring `__constructor` parameters for classes as well as for scalar types that have default values.
4. Supports PHP ^7.4|^8.
5. Allow you following the best practices for developing your code.
6. No PHPCS conflicts.
7. Supports Lazy Load class instantiating.
8. Lightweight and fast.

How to install in a project
---------------------------

[](#how-to-install-in-a-project)

There are several ways, you can choose what you more prefer:

### Install via Composer

[](#install-via-composer)

```
composer require renakdup/simple-php-dic
```

### Copy and paste file

[](#copy-and-paste-file)

1. Just copy the file `./src/Container.php` to your plugin directory or theme.
2. Rename `namespace` in the file
    from `Renakdup\SimpleDIC` to `\SimpleDIC`
3. Include the file.

How to use it
-------------

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

### Get started:

[](#get-started)

1. Create container instance.
2. Set a service
3. Get a service
4. Use object

```
use Renakdup\SimpleDIC\Container;

// create container
$container = new Container();

// set service
$container->set( Paypal::class, function () {
    return new Paypal();
} );

// get service
$paypal = $container->get( Paypal::class );

// use this object
$paypal->pay();
```

Method `get()` works as a singleton. Service will be resolved once and cached inside the `$container`, new invocations of the `get()` method return the same object.

```
$obj1 = $constructor->get( Paypal::class );
$obj2 = $constructor->get( Paypal::class );
var_dump( $obj1 === $obj2 ) // true
```

If you want to instantiate a service several times, then use the `make()` method.

```
$obj1 = $constructor->make( Paypal::class );
$obj2 = $constructor->make( Paypal::class );
var_dump( $obj1 === $obj2 ) // false
```

---

### Factory

[](#factory)

Factory is an `anonymous function` that wrap creating an instance.
Allows to configure how an object will be created and allows to use `Conainer` instance inside the factory.

```
$container->set( Paypal::class, function () {
    return new Paypal();
} );
```

As well factories create objects in the Lazy Load mode. It means that object will be created just when you resolve it by using `get()` method:

```
$container->set( Paypal::class, function () {
    return new Paypal();
} );

$paypal = $constructor->get( Paypal::class ); // PayPal instance created
```

---

### Primitives

[](#primitives)

SimpleDIC allows setting values for the container for primitive types:

```
$container->set( 'requests_limit', 100 );
$container->set( 'post_type', 'products' );
$container->set( 'users_ids', [ 1, 2, 3, 4] );

$user_ids = $container->get( 'users_ids', [ 1, 2, 3, 4] );
```

---

### Container inside factory

[](#container-inside-factory)

**SimpleDIC** allows to get a `Container` instance inside a factory if you add parameter in a callback `( Container $c )`. This allows to get or resolve another services inside for building an object:

```
$container->set( 'config', [
    'currency' => '$',
    'environment' => 'production',
] );

$container->set( Paypal::class, function ( Container $c ) {
    return new Paypal( $c->get('config') );
} );
```

---

### Autowiring

[](#autowiring)

**SimpleDIС** autowiring feature **allows to `Container` automatically create and inject dependencies**.

I'll show an example:

```
class PayPalSDK {}
class Logger {}

class Paypal {
    public function __constructor( PayPalSDK $pal_sdk, Logger $logger ) {
        //...
    }
}
```

And then when you create `Paypal::class`, you run `$container->get(Paypal::class)`, and `Container` identifies all classes in the constructor and resolves them. As if it's:

```
new Paypal( new PayPalSDK(), new Logger() );
```

---

Container autowiring can resolve default values for *primitive* parameters in a constructor:

```
class Logger {
    public function __constructor( $type = 'filestorage', $error_lvl = 1 ) {
        //...
    }
}
```

You can use **auto-wiring** feature that allows to `Container` create an instances that requires in the `__constructor` of class as well as it resolves constructor dependencies for

Note

But if object creating is more complex and requires configuring and you don't have parameters with default values in the constructor then you need to use `factory` for preparing service.

---

### Create an instance every time

[](#create-an-instance-every-time)

Method `make()` resolves services by its name. It returns a new instance of service every time and supports auto-wiring.

```
$conatainer->make( Paypal::class );
```

Note

Constructor's dependencies will not instantiate every time.
If dependencies were resolved before then they will be passed as resolved dependencies.

Consider example:

```
class PayPalSDK {}

class Paypal {
    public PayPalSDK $pal_sdk;
    public function __constructor( PayPalSDK $pal_sdk ) {
        $this->pal_sdk = $pal_sdk;
    }
}

// if we create PayPal instances twice
$paypal1 = $container->make( Paypal::class );
$paypal2 = $container->make( Paypal::class );

var_dump( $paypal1 !== $paypal2 ); // true
var_dump( $paypal1->pal_sdk === $paypal2->pal_sdk ); // true
```

Dependencies of PayPal service will not be recreated and will be taken from already resolved objects.

---

PSR11 Compatibility
-------------------

[](#psr11-compatibility)

in progress

Roadmap
-------

[](#roadmap)

- Add binding services with configuration
- Add auto-wiring for registered classes in DIC
- Add auto-wiring for defaults primitives for auto-fillings
- Add supporting invocable classes
- Add PSR11 interfaces in the Container.php.
- Add auto-wiring support for not bounded classes.
- Add resolved service storage (getting singleton).
- Add ability to create new instances of service every time.
- Improve performance.
- Fix deprecated `Use ReflectionParameter::getType() and the ReflectionType APIs should be used instead.` for PHP8
- Bind $container instance by default.
- Add `remove` method.
- Circular dependency protector.

Nice to have
------------

[](#nice-to-have)

- Integrate CI with running autotests
- Add badges with tests passed
- Add PHPStan analyzer
- Add code coverage badge
- Add descriptions in the code for functions.
- Choose codestyle
- Add on packagist
- Add if class exists checks in the Container file?
- Rename Container.php to SimpleContainer.php
- Show stack trace when I have a debug only?
- PHP 8 named arguments and autowiring.
- Allow to set definitions via `__constructor`.
- Add supporting Code Driven IoC.
- Add configurations of Container.
- Add Performance Benchmarks

License
-------

[](#license)

[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

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

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity51

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 ~24 days

Total

12

Last Release

628d ago

Major Versions

0.2.6 → 1.0.02024-05-12

PHP version history (2 changes)0.2.0PHP ^7.4

0.2.6PHP ^7.4|^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/d3f11053e005f02e0dc3049c010b45034646963c7af8edac8076c3767022d750?d=identicon)[renakdup](/maintainers/renakdup)

---

Top Contributors

[![renakdup](https://avatars.githubusercontent.com/u/9306079?v=4)](https://github.com/renakdup "renakdup (76 commits)")

---

Tags

dependency-injection-containerdidi-containerdicdicontainerphpphp-di-containerphp-dicphpdiphpdicsimple-di-containersimple-dicwordpresswordpress-diwordpress-dicwordpress-pluginwordpress-themewordpressdi containerdicwpwordpress pluginmu-pluginwordpress-libraryWP Pluginsimple dicdic autowiringsimple php dicwordpress dicwordpress di containerwordpress mu-plugin

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/renakdup-simple-dic/health.svg)

```
[![Health](https://phpackages.com/badges/renakdup-simple-dic/health.svg)](https://phpackages.com/packages/renakdup-simple-dic)
```

###  Alternatives

[varunsridharan/wp-dependencies

Provides Function To Check if a plugin is active/inactive &amp; function to compare versions.

1032.5k1](/packages/varunsridharan-wp-dependencies)[wpstarter/framework

The WpStarter Framework - Laravel Framework for WordPress

1810.1k4](/packages/wpstarter-framework)

PHPackages © 2026

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