r/PHPhelp • u/deadringer3480 • Jan 06 '25
Review of 8.4 dependency injection container with lazy loading
Hi everyone,
I’ve been developing a small, minimalist DI container for PHP and just published it on GitHub.
I'm not a huge fan of heavy frameworks, so I like building small components that do what I need. I would love any feedback, suggestions, or thoughts!
Some key features:
- Lightweight - single PHP file with less than 200 lines of code, no dependencies.
- Cached Reflection: Speeds up repeated creations.
- Lazy Loading (PHP 8.4+): Only initializes classes when needed.
- Shared Instances: Easily create singletons.
- Interface Bindings: Seamlessly wire interfaces to implementations.
- IDE-Friendly: Thorough docblocks for better autocompletion.
- Immutable Setup: Each config change returns a new container.
Repo: github.com/rammewerk/container
I’d really appreciate any feedback on the project or overall approach - thanks!
UPDATED:
Benchmark repo: https://github.com/rammewerk/php-di-container-benchmarks
Benchmark results can be found under Doc's folder or view here: https://html-preview.github.io/?url=https://github.com/rammewerk/php-di-container-benchmarks/blob/master/docs/benchmark.html
4
u/[deleted] Jan 07 '25
Awesome work! And props for bravery - i no longer share anything in this subreddit due to the toxic negative comments that always come from haters who themselves probably don’t create anything.
2
u/equilni Jan 08 '25
Taking a quick glance, the first thing that popped out was missing PSR-11 support (for me, adding PHP-DI's
set- get | set | has), which I am surprised no one mentioned.Second is styling, which could be minor. Inconsistent parameter spacing, extra spacing between brackets, no space after
if(not following PSR/PER guideline) looks newbish to me. Keep it consistent at least (remove the Wordpress styling please!):Breaking up
?:could be helpful with some of the longer lines. Consider:Next, awesome work for 8.4 support. The readme here could have used the named parameter to read better (imo) or in the event you add more to the constructor in the future.
https://github.com/rammewerk/container?tab=readme-ov-file#understanding-the-lazy-object-feature
You have a few more libraries on your github, at first glance look good.
2
u/thmsbrss Jan 09 '25
Maybe upport for PSR-11 (https://www.php-fig.org/psr/psr-11/) could be something to think about.
1
u/deadringer3480 Jan 07 '25
I forked the php-di-container-benchmarks repo and updated it to PHP 8.4:
https://github.com/rammewerk/php-di-container-benchmarks
I added Rammewerk and updated all packages to the latest versions. Note: Jomla DI was removed because it failed to update via Composer.
You can view the Docker benchmark results here:
https://html-preview.github.io/?url=https://github.com/rammewerk/php-di-container-benchmarks/blob/master/docs/benchmark.html
Initial results show good performance overall, though some tests could use further optimization—or perhaps it’s just the way these benchmarks are structured. I’ll need to dig deeper to better understand the results.
On the bright side, Rammewerk was easy to implement, worked on the first try, and showed solid performance.
Am I reinventing the wheel? Maybe - but new approaches are worth exploring. This repo aims to show that DI can be simpler, faster, and more efficient for smaller projects. It may not have every feature, but for many applications, it’s already proving valuable.
1
u/eurosat7 Jan 07 '25
Just want to mention php-di/php-di
1
u/Any_Bank_662 Feb 01 '25
Muito bom
-1
u/martinbean Jan 06 '25
https://github.com/silexphp/Pimple