r/PHPhelp • u/Successful-Emoji • Mar 08 '25
Solved Difficulties using PHP-DI to handle implentations
I am working on a school project (no worries, I am not asking for doing it for me) that asks me to write a website in PHP. I decided to use PHP-DI as my dependency injection library. I have the following code (that aims) to decide how my scripts detect the logged in user:
However, I can't instantate a class when I add the following in my class:
It gives me the following error:
Uncaught DI\Definition\Exception\InvalidDefinition: Entry "EMO\Selflearn\emoSessionDetector" cannot be resolved: the class is not instantiable
Full definition:
Object (
class = #NOT INSTANTIABLE# EMO\Selflearn\emoSessionDetector
lazy = false
) in /var/www/html/project/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:19
Stack trace:
#0 /var/www/html/project/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(109): DI\Definition\Exception\InvalidDefinition::create(Object(DI\Definition\ObjectDefinition), 'Entry "EMO\\Self...')
#1 /var/www/html/project/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(56): DI\Definition\Resolver\ObjectCreator->createInstance(Object(DI\Definition\ObjectDefinition), Array)
#2 /var/www/html/project/vendor/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php(60): DI\Definition\Resolver\ObjectCreator->resolve(Object(DI\Definition\ObjectDefinition), Array)
#3 /var/www/html/project/vendor/php-di/php-di/src/Container.php(354): DI\Definition\Resolver\ResolverDispatcher->resolve(Object(DI\Definition\ObjectDefinition), Array)
#4 /var/www/html/project/vendor/php-di/php-di/src/Container.php(136): DI\Container->resolveDefinition(Object(DI\Definition\ObjectDefinition))
#5 /var/www/html/project/src/emoMaintenanceScriptRun.php(83): DI\Container->get('EMO\\Selflearn\\e...')
#6 /var/www/html/project/run.php(18): Emo\Selflearn\emoMaintenanceScriptRun->run()
#7 {main}
thrown in /var/www/html/project/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 19
// ... (it repeated multiple times with the exact same content but different heading.)
However, web entry (i.e. emoHTTPSessionDetector) seemed unaffected, i.e. they can get a emoHTTPSessionDetector despite using basically the same injection code. After some debugging on the console entrypoint, I found the following intresting fact:
Note that the session detector should be a singleton as long as it is handling the same request. How can I solve this issue?
Note: I am not sure if I can share the whole project, so I didn't attach a link to it. If any snippets is needed for tackling the problem, feel free to ask me, and I will provide them with private and obviously unrelated contents omitted.
Edit: And after some further investigations, I figured out that this code succeed, where emoMaintenanceScriptRun is yet another class that uses the injection syntax described above:
But this failed:
View all comments
Show parent comment
1
u/Successful-Emoji Mar 08 '25
Oh god, almost forgot. It solved me problem - thanks, that's so silly.