Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
  • 01:54
    hengfeng-su starred PHP-DI/PHP-DI
  • Jun 29 15:10
    pbojan starred PHP-DI/PHP-DI
  • Jun 29 08:53
    antfig starred PHP-DI/PHP-DI
  • Jun 29 08:47
    watain666 starred PHP-DI/PHP-DI
  • Jun 28 09:37
    mnapoli labeled #720
  • Jun 28 07:22
    Neilyoz starred PHP-DI/PHP-DI
  • Jun 26 04:52
    leemcd56 starred PHP-DI/PHP-DI
  • Jun 25 19:22
    jamesaspence closed #720
  • Jun 25 19:22
    jamesaspence commented #720
  • Jun 25 19:19
    jamesaspence commented #720
  • Jun 25 19:18
    jamesaspence commented #720
  • Jun 25 18:56
    morith starred PHP-DI/PHP-DI
  • Jun 25 17:24
    JoeBengalen commented #720
  • Jun 25 16:43
    jamesaspence opened #720
  • Jun 24 09:04
    weikilla starred PHP-DI/PHP-DI
  • Jun 23 05:55
    gilbertchiao starred PHP-DI/PHP-DI
  • Jun 22 19:29
    gbaims starred PHP-DI/demo
  • Jun 21 19:59
    carlosruesta starred PHP-DI/demo
  • Jun 19 06:02
    HavenShen starred PHP-DI/PHP-DI
  • Jun 18 13:34
    texnicii starred PHP-DI/PHP-DI
Model class code listed below.
when i use constructor way (commented) without any definitions everything works good, but i want to make possible to use it outside container so i want free constructor from the container dependency. later method will be renamed to smth like getContainerConnection and getConnection will be without container dependency then it all will be refactored and so on ... but for now i need to release constructor and create some definition to instruct DI container on how Model class should be created like: should be called setContainer method and fed with the Container itself after that should be called getConnection and returned instance of Model
    public function __construct()

    public function setContainer(ContainerInterface $container)
        $this->container = $container;

        return $this;

    public function getConnection()
        $dbType = ucfirst($this->container->get('db.type'));

        $class = self::DB_NAMESPACE . $dbType;

        try {
            $this->db = $this->container->get($class)->getConnection();
        } catch (\Exception $e) {
            echo $e->getMessage();

        return $this->db;
        public function __construct(ContainerInterface $container)
            $dbType = ucfirst($container->get('db.type'));
            $class = self::DB_NAMESPACE . $dbType;

            try {
                $this->db = $container->get($class)->getConnection();
            } catch (\Exception $e) {
                echo $e->getMessage();
Anaël Ollier

Hi there,
I have a question about \DI\get() and the documentation saying that it's a "alias" to any existing definition. The result I get is a bit misleading when dealing with non-singleton objects. Here's my example:

class UniqId {
    public $id;
    public function __construct() {
        $this->id = uniqid();
class OtherClass {
    public $id;
    public function __construct(UniqId $u) {
        $this->id = $u->id;
$containerBuilder = new DI\ContainerBuilder();
    UniqId::class       => \DI\create(),
    'UniqId_alias'      => \DI\get(UniqId::class),
    OtherClass::class   => \DI\autowire(),
    'OtherClass_alias'  => \DI\get(OtherClass::class),
    'OtherClass_alias2' => \DI\autowire(OtherClass::class)
        ->constructorParameter('u', \DI\get('UniqId_alias'))
$container = $containerBuilder->build();

    'UniqId            ' => $container->make(UniqId::class)->id,
    'UniqId (again)    ' => $container->make(UniqId::class)->id,
    'UniqId_alias      ' => $container->make('UniqId_alias')->id,
    'OtherClass        ' => $container->make(OtherClass::class)->id,
    'OtherClass_alias  ' => $container->make('OtherClass_alias')->id,
    'OtherClass_alias2 ' => $container->make('OtherClass_alias2')->id,

The result is as follows:

array (size=6)
  'UniqId            ' => string '5d0e6194728ec' (length=13)
  'UniqId (again)    ' => string '5d0e619472911' (length=13)
  'UniqId_alias      ' => string '5d0e61947294e' (length=13)
  'OtherClass        ' => string '5d0e61947294e' (length=13)
  'OtherClass_alias  ' => string '5d0e61947294e' (length=13)
  'OtherClass_alias2 ' => string '5d0e61947294e' (length=13)

My idea was that as UniqId is defined as a non-singleton because of \DI\create(), so were its references when used as dependencies of other classes (here OtherClass ot its aliases). But actually it's not. The only solution I've found is to "force" it when defining the dependent class:

'OtherClass_alias3' => \DI\autowire(OtherClass::class)
        ->constructorParameter('u', \DI\create(UniqId::class))
but this way, we loose the power of creating a particular definition only once.

To be more precise about what results I'm confused about, it's:

  'OtherClass        ' => string '5d0e6316dbd5a' (length=13)
  'OtherClass_alias  ' => string '5d0e6316dbd5a' (length=13)
  'OtherClass_alias2 ' => string '5d0e6316dbd5a' (length=13)

where clearly the instance of UniqId which is injected in OtherClass is a singleton, when I would like to have a new instance each time a new OtherClass is created.

Richard Quadling
Is there a way to force a new instance even if the developer calls get() rather than make()?
Howdy folks, does anyone have a rough comparison between php-di and other DI frameworks?
Simon Müller
ist the DI working with symfony 4?
hello, iI use php DI 6.0 and I have this error :
Uncaught DI\NotFoundException: No entry or class found
Where I could go to resolve this ?
I use php/DI v6.0.9
Some problem in there ?
Amir Roushenas
Is there any tutorial to build a sample application from scratch is available? The existing ones are too complicated for a new bee, and they just talked very generally...
Bram Van der Sype
@mnapoli Any suggestions on how to resolve circular dependency issues? I have a request handler that needs a command bus to handle a command, that command triggers an event that’s emitted on an event bus, which has an event listener that also requires the commandbus. I thought this wouldn’t be an issue, but apparently it is.
Anaël Ollier
@Brammm What you might want to try is to inject one of the dependencies via setter and not via constructor. This way you should be able to construct an (still incomplete) object, and then add the missing dependency, without falling into a CD issue. See
Hello, does anyone have a link with some documentation on how to use PHP definitions to instruct the container to create a new instance when resolving something out of the container (like make())? My attempts keep resulting in objects with the same spl_object_hash().
Anaël Ollier
@ed-iv In my understanding you can't. If you want to create a new instance, you must use make(), otherwise you'll get the same object as you observed.
Hey, @nanawel, thanks for the reply. What I tried to do was to register a factory class in the container which is responsible for creating new instances for me. Then I inject that factory class into my controllers via the constructor. However, even in this case it seems like when I use the factory within the constructor to generate a new instance I'm still just getting the same instance. Does that line up with your understanding too?
I take it back, after some additional testing this morning I see that the above strategy of registering a factory class in the container and then injecting it to fetch new instances is working as I had expected. Must have been doing something else wrong before.
Anaël Ollier
Yes, this is the right way to do it. Glad you figured it out :)
this is my test message
first time user of Slim4 framework and first time even trying to use a DI. Slim4 uses PHP-DI and i'm having a hell of a time figuring out how to use it. It seems to only inject SOME of the dependencies while ignoring others. How do i debug this or troubleshoot it? I've run "composer dump-autoload" to no avail, it has the classes mapped correctly. Everything is typehinted, and explicit "use blah/blah" exist as well. any help would be appreciated
Anaël Ollier
Hi @gmariani Is autowiring enabled? Could you post the configuration files you're using if any, or the initialization of the Container object?
== index.php

(require __DIR__ . '/app/config/bootstrap.php')->run();

== bootstrap.php

use DI\ContainerBuilder;
use Slim\App;
require_once __DIR__ . '/../../../vendor/autoload.php';

$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions(__DIR__ . '/container.php');
$container = $containerBuilder->build();
$app = $container->get(App::class);
(require __DIR__ . '/routes.php')($app);
(require __DIR__ . '/middleware.php')($app);
return $app;

== container.php

use Psr\Container\ContainerInterface;
use Selective\Config\Configuration;
use Slim\App;
use Slim\Factory\AppFactory;

return [
    Configuration::class => function () {
        return new Configuration(require __DIR__ . '/settings.php');

    App::class => function (ContainerInterface $container) {
        $app = AppFactory::create();
        return $app;

    PDO::class => function (ContainerInterface $container) {
      $config = $container->get(Configuration::class);
      return new PDO($dsn, $username, $password, $flags);

== settings.php

$settings = [];
$settings['root'] = dirname(__DIR__);
$settings['temp'] = $settings['root'] . '/tmp';
$settings['public'] = $settings['root'] . '/public';
$settings['error_handler_middleware'] = [
    'display_error_details' => true,
    'log_errors' => true,
    'log_error_details' => true,
$settings['db'] = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'flags' => [

return $settings;
Anaël Ollier
Well, seems like at least these parts are okay to me. I suppose the problem does not occur with the classes defined in your container.php? What is the error you're getting?
== routes.php


use Slim\Http\Response;
use Slim\Http\ServerRequest;
use Slim\App;

//require \App\Action\HomeAction;
//require __DIR__ . '/../action/HomeAction.php';
//require __DIR__ . '/../action/AccountReadAction.php';

return function (App $app) {
    $app->get('/', \App\Action\HomeAction::class);

    $app->get(      '/api/v1/accounts',     \App\Action\AccountReadAllAction::class);
    $app->post(     '/api/v1/account/new',  \App\Action\AccountCreateAction::class);
    $app->get(      '/api/v1/account/{id}', \App\Action\AccountReadAction::class);
    $app->patch(    '/api/v1/account/{id}', \App\Action\AccountUpdateAction::class);
    $app->delete(   '/api/v1/account/{id}', \App\Action\AccountDeleteAction::class);

== Error Message

Slim Application Error

The application could not run because of the following error:
Type: RuntimeException
Code: 0
Message: Callable App\Action\AccountReadAction does not exist
File: /home/PROJECTNAME/vendor/slim/slim/Slim/CallableResolver.php
Line: 137

#0 /home/PROJECTNAME/vendor/slim/slim/Slim/CallableResolver.php(91): Slim\CallableResolver->resolveSlimNotation('App\\Action\\Acco...')
#1 /home/PROJECTNAME/vendor/slim/slim/Slim/CallableResolver.php(61): Slim\CallableResolver->resolveByPredicate('App\\Action\\Acco...', Array, 'handle')
#2 /home/PROJECTNAME/vendor/slim/slim/Slim/Routing/Route.php(358): Slim\CallableResolver->resolveRoute('App\\Action\\Acco...')
#3 /home/PROJECTNAME/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\Routing\Route->handle(Object(Slim\Http\ServerRequest))
#4 /home/PROJECTNAME/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#5 /home/PROJECTNAME/vendor/slim/slim/Slim/Routing/Route.php(333): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#6 /home/PROJECTNAME/vendor/slim/slim/Slim/Routing/RouteRunner.php(80): Slim\Routing\Route->run(Object(Slim\Http\ServerRequest))
#7 /home/PROJECTNAME/vendor/slim/slim/Slim/Middleware/BodyParsingMiddleware.php(50): Slim\Routing\RouteRunner->handle(Object(Slim\Http\ServerRequest))
#8 /home/PROJECTNAME/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\BodyParsingMiddleware->process(Object(Slim\Http\ServerRequest), Object(Slim\Routing\RouteRunner))
#9 /home/PROJECTNAME/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): class@anonymous->handle(Object(Slim\Http\ServerRequest))
#10 /home/PROJECTNAME/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Http\ServerRequest), Object(class@anonymous))
#11 /home/PROJECTNAME/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(89): class@anonymous->handle(Object(Slim\Http\ServerRequest))
#12 /home/PROJECTNAME/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Http\ServerRequest), Object(class@anonymous))
#13 /home/PROJECTNAME/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(89): class@anonymous->handle(Object(Slim\Http\ServerRequest))
#14 /home/PROJECTNAME/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Http\ServerRequest), Object(class@anonymous))
#15 /home/PROJECTNAME/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): class@anonymous->handle(Object(Slim\Http\ServerRequest))
#16 /home/PROJECTNAME/vendor/slim/slim/Slim/App.php(208): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#17 /home/PROJECTNAME/vendor/slim/slim/Slim/App.php(192): Slim\App->handle(Object(Slim\Http\ServerRequest))
#18 /home/PROJECTNAME/public_html/index.php(5): Slim\App->run()
#19 {main}
So i included the routes.php which is the next step that would run, which you saw in the bootstrap.php earlier. But unless i manually put those includes (which are commented out) it gives me the above error message. But you can see it's not throwing errors for Slim\App or anything else, but it does for \App\Action\AccountReadAction
Anaël Ollier
I don't know Slim, but to me this looks more like an autoload issue than a DI issue. Are you sure you have an autoloader somewhere that's able to handle \App classes? Otherwise if it follows PSR-4 convention (which it should) you can add it to your composer.json like so:
In bootstrap.php I do have require_once __DIR__ . '/../../../vendor/autoload.php'; which is created by Composer. Here is my composer.json:
  "require": {
    "php": ">=7.1",
    "ext-json": "*",
    "monolog/monolog": "^2.0",
    "php-di/php-di": "^6.0",
    "selective/config": "^0.1.1",
    "slim/http": "^0.8.0",
    "slim/psr7": "^0.6.0",
    "slim/slim": "4.*"
  "require-dev": {
    "phpunit/phpunit": "8"
  "autoload": {
    "psr-4": {
      "App\\": "public/app/"
Anaël Ollier
So I assume your class \App\Action\HomeAction is located in public/app/Action/HomeAction.php, right? (just to be sure)
Sorry I won't be able to help you more for today (you know, Xmas and stuff) but I'll try to come back in a few days. In the meantime, try to use the debugger to trace how files are (auto)loaded and classes are resolved, you might figure out what's wrong more easily.
thanks for looking atleast. this is my first time using a php framework (which requires composer and php-di) so it's a lot all at once. no one does things simple anymore.. thanks again
oh and yes, you are correct, that is the path of the file
Anaël Ollier
It's a lot indeed. I've had my hard times too working with PHP-DI at first, but there's no magic int it and usually it's a PBKAC :)
that's my guess as well, but there is little documentation for people starting off. everyone assumes you know what you're doing. i didn't even know php-di was a thing until i started this project
Anaël Ollier
Trust me, once you'll get used to it, you won't want to do without ;)
The learning curve is just a bit steep
i usually can figure this out, but i'm just hitting a brick wall and don't know where to look for help at this point. can't tell if it's a slim issue, php-di issue (which you don't seem to think so) so i guess it's just how i have configure composer (autoload)
i have to say it's really tempting to just say screw it and just manually put the require lines in and be done with it
Does PHP-DI actually include the files or is that composer?
I figured out the issue. My project had the folder "public" but the web server had "public_html". once i fixed the path issue the problem was resolved. thanks for your help @nanawel
Anaël Ollier
Hi @gmariani. Glad you figured this out! So that was a path issue after all, nothing wrong with PHP-DI :)
Daniel Opitz
@gmariani It looks like an PSR-4 autoloader issue. Make sure that you defined the correct namespaces and paths in your composer.json file. Linux is case sensitive. For example if you have a namespace App\Action, then your directory should be named like this: src/Action/ and not src/action/.
Jack Price-Burns
Hello :wave: when using the autowiring to load classes, if the class can not be instantiated because of an exception the container just returns null, is this behaviour intended (I don't know if I've changed something) and is there any way I can make these exceptions get thrown out of the container so I can handle them myself?

Hi. I have this problem, when I upload my project to a host this error activates me: Fatal error: Not captured DI \ NotFoundException: No entry or class was found for 'App \ Controllers \ AuthController' in /home/vrfctuqw/ vendor /php-di/php-di/src/Container.php:135 Stack tracking: # 0 /home/vrfctuqw/ : DI \ Container-> get ('App \ Controllers ...') # 1 /home/vrfctuqw/ DI \ CompiledContainer-> get ('App \ \ Controllers ... ') # 2 {main} included in /home/vrfctuqw/ on line 135

Locally it works correctly, but in hosting I present this. I would like you to help me please

Định Nguyễn Thế
I has a problem when i call a magic menthod in a class.
Sandunika Fernando
Hi I have used "mnapoli/php-di": "~4.0", "container-interop/container-interop": "^1.1", for my application and now im moving to php 7 with "php-di/php-di": "6.0.11", "psr/container": "^1.0.0", I'm getting "Entry "Idfix\Contracts\RegistryInterface" cannot be resolved: the class is not instantiable Full definition: Object ( class = #NOT INSTANTIABLE# Idfix\Contracts\RegistryInterface lazy = false )" this issue please advice
Stay The Fuck Home
Hello! So, after doing composer update I get a notice that the package jeremeamia/superclosure is abandoned and php-di seems to require/depend on this package. Any solutions to this problem especially that the recommendation is using opis/closure and it's not advisable to edit composer.lock directly??