-
-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intercep class loading without Container #560
Comments
Hi, I think you may be confusing class loading (i.e. autoloading, what Composer does) and object instantiations ( PHP-DI instantiates classes, but it does do any autoloading. What you may have in mind is some kind of "overriding" of I know migrating an existing legacy application to these kinds of patterns is not easy. There are some things that could help, for example if you are using the singleton pattern: change the |
Well, in other languages I worked with, this was the default behavior. It's very painfull to have to pass a I think that PHP-DI receives some qualifyied class name and then (after some reflection stuff) instantiates it with some parameters, letting the loading job with Composer, right? |
Again you are confusing class loading and object instantiation, those are very different things. Also you should not pass the You can read http://php-di.org/doc/best-practices.html to learn more. |
I know what an loading class/function does (I've made a lot of them before using Composer), I know what is instantiation and you didn't answer my simple question. Also the docs doesn't have any complete example from setting the container up to instantiating a class where something will be injected by PHP-DI (yes, I already read the best practices section). I can't figure out how it works, cause there's isn't a universal pattern for using some DI package, from what I read here I understand that I have to simple write |
Well there is the Getting started guide which does just that, does it help? (especially the section
Yes that's it. Read the getting started guide, especially this section for more details: http://php-di.org/doc/getting-started.html#framework-integration
What is your question exactly? If this is this:
Then yes, the answer is yes: composer will |
Nice, thanks. 😄
Have no idea how the hell I could miss this. I'll test this functionallity tonight, thanks for the help.
Ok. I have two use cases, one with Slim + Pimple (which I want to remove) and one with native PHP code, both in PHP 7.1. |
OK regarding Slim it should be easier with the bridge: http://php-di.org/doc/frameworks/slim.html For transitioning from Pimple to PHP-DI you can take the closures you defined in Pimple and simply put them in PHP-DI like this: http://php-di.org/doc/php-definitions.html#factories |
I've done the Slim integration before, but I reverted everything, because I made my entire project dependent from Slim and I don't want that. I'm trying to implement PHP-DI in a native way so then my Slim router can just use PHP-DI as a plus provided by my application scenario. Because of that, the Slim integration will take longer to work for me. Thanks for the usefull links. |
I'd like to add that usually frameworks that allow DI containers, and Slim in particular, use that container in two distinct places to fetch objects from it: First in order to get all necessary objects to bootstrap processing an incoming request in general (which would for example create the PSR-7 request and response objects, the router, etc.), and the second part takes place if the requested controller has been identified. If you feel the need to pass the container through multiple objects in order to fetch something deeper inside:
It'll take some experience to fully grasp the concept, but with Slim you basically only have to pass the DI container into the |
Well, I tryied to use the empty container get (
|
I did not mean literally calling You should really use a framework integration first I think, that will be easier to get started. |
Funny how this is confusing for beginners to understand IoC concepts when the only thing you know is some framework that magically "just works". I hope my lack of knowledge helps someone through this thread and, if you need any help understanding IoC, please ping me. |
It's too much unusual to have to use a
$container
var to load every class. I think there's could be a way to use the DI functionality without having to rewrite the entire application to load classes through a$container
, maybe Composer's autoload could be intercepted or something else.The text was updated successfully, but these errors were encountered: