1

I use php-di for autoloading classes in my API-Application.

For speed, however, php-di states that you have to autowire the classes manually for compilation. So they give us this example:

return [
  // ... (your definitions)

  UserController::class => autowire(),
  BlogController::class => autowire(),
  ProductController::class => autowire(),
  // ...
];

I do not understand which classes we have to autowire for compilation. All classes? Or only the controllers?

As the application is pretty large, I can imagine how big this DiC config file will become. Not only that, but what if I create a new repository or model, then I should not forget to add it to my DiC config..

source: http://php-di.org/doc/performances.html

sridesmet
  • 875
  • 9
  • 19

1 Answers1

1

You could add only your controller: PHP-DI will loop recursively through all their dependencies (and their dependencies) to compile them. Controllers are usually the entrypoint of the application so it's a good starting point for finding all (or most) services in the application.

I think however it's more of a matter of performances. If your application has super short response times then maybe it's worth it, else adding all your classes for compilation may not really make a visible difference.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • So you're saying if I add all classes (not only the services/controllers), it will make a speed difference (even it's supersmall)? Because if it's looping recursively, it shouldn't make any difference, right? – sridesmet Apr 06 '18 at 14:31
  • 1
    If a controller takes service `X` as a dependency, and that service `X` takes repository `Y` as a dependency, then simply adding the controller to the config will compile all 3 classes. However if there is a service `Z` that is not a dependency of a controller, then it will not be compiled. So you *could* add all the controllers, services and other classes to compile everything, but maybe (probably?) it's not worth it. I would say measure first before spending too much time into that. Maybe adding only the controllers is enough. – Matthieu Napoli Apr 06 '18 at 16:39