meow/di

Dependency injection container

Maintainers

Details

github.com/meowphp/di

Source

Issues

Installs: 13

Dependents: 1

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/meow/di

v0.1.1 2025-04-18 10:11 UTC

This package is auto-updated.

Last update: 2025-09-18 10:57:47 UTC


README

namespace: meow\di

Dependency injection container for PHP

ko-fi

Installation

To install this plugin use following command

composer require meow/di

Usage

Example how to use this container

Creating new container

To create new container you can use code as follows

$container = new ApplicationContainer();

Registering services

Services are defined as array [interface => class] and have to be defined before you can resolve your classes.

protected array $services = [
    BaseServiceInterface::class => BaseService::class
];

// add services to container
foreach ($this->services as $k => $v) {
    $container->set($k, $v);
}

Resolving classes

With DI container you don't need to create new instances in constructor (as in example controller - check tests).

class MainController
{
    protected User $user;

    protected BaseServiceInterface $service;

    public function __construct(BaseServiceInterface $service, User $user)
    {
        $this->user = $user;
        $this->service = $service;
    }
}

now you can resolve controller with container

/** @var MainController $controller */
$controller = $container->resolve(MainController::class);

License: MIT