PHPackages                             nezamy/di - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. nezamy/di

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

nezamy/di
=========

Dependency Injection and container

v2.0(5y ago)2178MITPHPPHP &gt;=7.4

Since Oct 2Pushed 3y ago2 watchersCompare

[ Source](https://github.com/nezamy/di)[ Packagist](https://packagist.org/packages/nezamy/di)[ Docs](https://github.com/nezamy/di)[ RSS](/packages/nezamy-di/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Dependency Injection
====================

[](#dependency-injection)

Dependency Injection and container

Installation via composer
-------------------------

[](#installation-via-composer)

```
composer require nezamy/di

```

Then load composer autoload

```
require __DIR__ . '/vendor/autoload.php';

```

Usage
-----

[](#usage)

Let's say we have a `Book` class, and we need to call `getName` method. Whatever the method is static or not.

```
class Book
{
    private string $name = 'First Book';

    public function getName(): string
    {
        return $this->name;
    }
}

$resolver = new Just\DI\Resolver;
$resolver->resolve($resolver->prepare([Book::class, 'getName']));
//Or
$resolver->resolve([new Book, 'getName']);
// the both returns 'First Book'
```

Maybe you call Book without `new` instance you should use `prepare` method like the first one.

### Call method with parameter

[](#call-method-with-parameter)

Now we have new method with one parameter

```
class Book
{
    private string $name = 'First Book';

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

$container = Just\DI\Container::instance();
$container->setVar('name', 'PHP');

$book = new Book();
$resolver = new Just\DI\Resolver;
$resolver->resolve([$book, 'setName']);
$book->getName();
// will return 'PHP'
```

We call th `setName` without the parameter, but we set `name` in our container above as a global variable. The resolver will search in the container and if got a variable match the same name of the parameter then pass it, if not will pass null.

#### Object type parameter &amp; singleton

[](#object-type-parameter--singleton)

```
$function = function(Book $book){
    return $book->getName();
};

$container = Container::instance();
$resolver = new Resolver;
$name = $resolver->resolve($function);
//here $name returns 'First Book' because it's initial value

$book = new Book();
$book->setName('Test Book');
// for define a singleton object
$container->set(Book::class, $book);

$resolver = new Resolver;
$name = $resolver->resolve($function);
$this->assertSame('Test Book', $name);
//$name now is equal 'Test Book'
```

### Call a method in a class has constructor, and the constructor needs two parameters

[](#call-a-method-in-a-class-has-constructor-and-the-constructor-needs-two-parameters)

```
class User{
    private string $name;
    private string $email;

    public function __construct(string $name, string $email)
    {
        $this->name = $name;
        $this->email = $email;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getEmail(): string
    {
        return $this->email;
    }
}

$container = Just\DI\Container::instance();
$container->setVar('name', 'Mahmoud Elnezamy');
$container->setVar('email', 'mahmoud@nezamy.com');

$resolver = new Just\DI\Resolver;
$name = $resolver->resolve($resolver->prepare([User::class, 'getName']));
// name here will return 'Mahmoud Elnezamy'
```

### Magic Call

[](#magic-call)

```
class User{
    private string $name;
    private string $email;

    public function __construct(string $name, string $email)
    {
        $this->name = $name;
        $this->email = $email;
    }

    public function getName(): string
    {
        return $this->name;
    }
}
class Book
{
    public function Auther(User $user){
        return $user->getName();
    }
}

$container = Just\DI\Container::instance();
$container->setVar('user', ['Mahmoud', 'email@domain.com']);
$container->setMagicCall(User::class, function ($attr, $value){
    return new User(...$value);
});

$resolver = new Just\DI\Resolver;
$book = $resolver->resolve(
    $resolver->prepare([Book::class, 'Auther'])
);
//$book will return 'Mahmoud'
```

API
---

[](#api)

```
$container = \Just\DI\Container::instance();

$container->setVar('name', 'value');
$container->getVar('name');
$container->hasVar('name');
$container->importVars([
    'name' => 'name here',
    'id' => '1'
]);

$container->set('className', new stdClass());

//Maybe the new instance do some processing or load some configurations or connect with database.
//and you won't to make the instance until the first use or call
$container->set('className', function (){
    return new stdClass();
});
$container->get('className');
$container->has('className');
// define some singleton objects
$container->import([
    Request::class => new Request(...),
    Response::class => new Response(...),
    DB::class => new DB('user', 'pass',...)
]);

$container->setMagicCall('UserModel', function ($attr, $value){
    if($attr == 'id'){
       return new UserModel($value);
    }
    return null;
});
$container->getMagicCall('UserModel');
$container->hasMagicCall('UserModel');
```

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

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

2099d ago

### Community

Maintainers

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

---

Top Contributors

[![nezamy](https://avatars.githubusercontent.com/u/3946147?v=4)](https://github.com/nezamy "nezamy (5 commits)")

---

Tags

containerdependencyinjectionjustframework

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/nezamy-di/health.svg)

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

###  Alternatives

[league/container

A fast and intuitive dependency injection container.

86894.4M437](/packages/league-container)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2860.1k2](/packages/capsule-di)[miladrahimi/phpcontainer

Dependency injection (IoC) container for PHP projects

1324.1k2](/packages/miladrahimi-phpcontainer)

PHPackages © 2026

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