PHP-DI creating new instances of objects instead of injecting the same instance everywhere

Stumbled on an issue today where PHP-DI was creating new instances of objects for autowiring and get() function instead of injecting the same instance everywhere. This was difficult to debug, since there’s not a lot of information about the library online. As always, looking at the github issues page led me to the solution.

I was prepending a backslash to the namespaces of objects:

$messageBag = $this->container->get('\Plasticbrain\FlashMessages\FlashMessages');

But apparently the FQN should not contain a leading slash. This is the correct way:

$messageBag = $this->container->get('Plasticbrain\FlashMessages\FlashMessages');

Hope this saves someone an hour or two.

Indrek Kõnnussaar

I'm a veteran Wordpress developer, context-driven tester, security enthusiast and the mastermind behind Codelight. I love building stuff that works and fixing stuff that doesn't.

Write me directly indrek@codelight.eu
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×