PHPackages                             bigbit/oddin - 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. bigbit/oddin

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

bigbit/oddin
============

On Demand Dependency INjection

v2.1.0(6y ago)050[3 issues](https://github.com/andrzejenne/oddin/issues)[1 PRs](https://github.com/andrzejenne/oddin/pulls)2MITPHPPHP ~7.4.0

Since Jul 12Pushed 6y agoCompare

[ Source](https://github.com/andrzejenne/oddin)[ Packagist](https://packagist.org/packages/bigbit/oddin)[ RSS](/packages/bigbit-oddin/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (9)Dependencies (8)Versions (14)Used By (2)

ODDIN - On Demand Dependency INjection
======================================

[](#oddin---on-demand-dependency-injection)

About
-----

[](#about)

Sometimes coding is a pain. Your boss needs it now or simply you are bored writing all sugar again and again and ...

If you are using DI in your projects, you have to write property declarations and initialize them in constructors. You can use dependency container or define injectable constructor arguments. It depends on framework used. For of PHP7.4.0 only, 7.4.1 breaks functionality, php bug #78904.

```
class Foo {
    private Dependency1 $dep1;

    private Dependency2 $dep2;

    private Depencency3 $dep3;

    function __construct(Dependency1 $dep1, Dependency2 $dep2, Container $container)
    {
        $this->dep1 = $dep1;
        $this->dep2 = $dep2;
        $this->dep3 = $container->get(Dependency3::class);
    }

    public function bar()
    {
        $this->dep1->doSomething();
    }

    protected function baz()
    {
        $this->dep2->doSomethind();
    }

    private function bye()
    {
        $this->dep3->doSomething();
    }
}
```

With ODDIN you can skip constructor part. Just declare properties and access properties anytime you need. Keep in mind those properties becomes accessible from anywhere by magic \_\_get method. Your preferred IDE will help you deal with that problem

```
class Foo {
    use InjectsOnDemand;

    private Dependency1 $dep1;

    private Dependency2 $dep2;

    private Depencency3 $dep3;

    /**
    * Foo constructor.
    * as of php7.4.1, we have to unset properties in constructor
    * https://bugs.php.net/bug.php?id=78904
    */
    public function __construct()
    {
        unset($this->dep1, $this->dep2, $this->dep3);
    }

    public function bar()
    {
        $this->dep1->doSomething();
    }

    private function baz()
    {
        $this->dep2->doSomething();
    }

    private function bye()
    {
        $this->dep3->doSomethind();
    }
}
```

How it works
------------

[](#how-it-works)

PHP classes can have magic methods. The \_\_get magic method is invoked every time you want to use unset property. DIResolver uses parser to get dependency metadata from class or deprecated property annotations. InjectsOnDemand trait defines magic \_\_get method, which handles all our property requests. Once property is initialized by trait, magic method is not called again.

Pros
----

[](#pros)

- less coding
- dependency instantiation on demand (lazy - not before constructor, if properly defined in DI container)

Cons
----

[](#cons)

- all properties becomes public ? all injectables are "public"
- antipattern ? use it only for prototyping, clean the code later.

Purpouse
--------

[](#purpouse)

Cleaner controller classes, less resource demanding. But it's up to you, where you use ODDIN.

Known Issues
------------

[](#known-issues)

- no code fixer yet

Quick start
-----------

[](#quick-start)

You can use any DI container, which implements Psr\\Container\\ContainerInterface. For quick start, you can use Bootstrap class, which uses SmartContainer.

```
use BigBIT\DIBootstrap\Bootstrap;
use Psr\Container\ContainerInterface;

// custom bindings
$bindings = [
    FooInterface::class => function(ContainerInterface $container) {
        return new BarImplementation(
                $container->get(BazDependency::class)
            );
        }
];

$container = Bootstrap::getContainer($bindings);

$app = new SomeApp($container);

$app->run();
```

### Other frameworks

[](#other-frameworks)

You can request other frameworks support or write you own bootstrap based on Bootstrap class.

PHP-DI comparison
-----------------

[](#php-di-comparison)

@todo

Cache Generator
---------------

[](#cache-generator)

Experimental implementation of cache generator was added. If your project has phpstan installed, it's recommended to install tracy/tracy as well. Oddin uses Psr\\SimpleCache\\CacheInterface implementations and Symfony as default.

Creating cache is not necessary, but recommended for production environments:

```
vendor/bin/oddin cache:injectables:create php-files -a oddin -a 0 -a cache
```

Arguments for cli commands are derived from adapter constructor.

Instantiating cache:

```
$bindings[CacheInterface::class] = function(ContainerInterface $container) {
    return new Psr16Cache(new PhpFilesAdapter('oddin', 0, dirname(__DIR__) . '/cache'));
};
```

@TODO - Code Fixer
------------------

[](#todo---code-fixer)

Cli command for fixing code. It will remove class property annotations, declare properties and add constructor or getters and container to constructor.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

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

Recently: every ~12 days

Total

12

Last Release

2307d ago

Major Versions

v0.2.0-alpha → v1.0.02019-09-04

v1.0.2 → v2.0.02019-12-02

1.0.x-dev → 2.1.x-dev2020-01-20

PHP version history (4 changes)v0.0.1-alphaPHP ^7.2.0

v2.0.0PHP ^7.4.0

2.0.x-devPHP 7.4.0

2.1.x-devPHP ~7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/30d24aafad042ca1644f0599dbb6dea333601b666179b4e690bf4e357e70e685?d=identicon)[lamasutra](/maintainers/lamasutra)

---

Top Contributors

[![andrzejenne](https://avatars.githubusercontent.com/u/15650034?v=4)](https://github.com/andrzejenne "andrzejenne (17 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bigbit-oddin/health.svg)

```
[![Health](https://phpackages.com/badges/bigbit-oddin/health.svg)](https://phpackages.com/packages/bigbit-oddin)
```

###  Alternatives

[mobiledetect/mobiledetectlib

Mobile\_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

10.7k159.4M431](/packages/mobiledetect-mobiledetectlib)[illuminate/contracts

The Illuminate Contracts package.

704122.9M10.1k](/packages/illuminate-contracts)[phiki/phiki

Syntax highlighting using TextMate grammars in PHP.

3573.0M23](/packages/phiki-phiki)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[flow-php/etl

PHP ETL - Extract Transform Load - Abstraction

374468.4k51](/packages/flow-php-etl)[gehrisandro/tailwind-merge-php

TailwindMerge for PHP merges multiple Tailwind CSS classes by automatically resolving conflicts between them

1391.5M9](/packages/gehrisandro-tailwind-merge-php)

PHPackages © 2026

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