1

Couldn't find this info in google or by asking people in discord programming servers. Before I ask the main question I wanna confirm something first:

When java runs in tomcat, lets say like in case of Spring Boot application with Tomcat built in it:

  1. all the components managed by ioc container: @controller/@configuration/@service, and other annotations that indicate that class should be managed by the container, do all these components get instantiated in container on startup of the application, before any http request comes from the client ? All the different controllers for many different, routes, do they get instantiated before they are actually needed to process any request ?
  2. if so, do all these instances of these components get used to process all subsequent incoming requests ? like a controller instance wouldnt be destroyed by GC after a request was processed and response was returned to client ? this controller is always kept in memory(heap I guess) until application stops running ?
  3. when a new http request comes to tomcat from client, does some new thread get created to process this request ? does this thread use the same instance of controller and other components managed by container to process the request ?

Now main question.

In PHP the whole new process is started when server starts index.php file and passes the request to it to process it. And php programs also have ioc containers, PDOs, repositories, etc - the components that are manage by container. Container can be self made by developers or provided by FW like symfony, laminas, laravel.

Does this mean that whenever a request is processed by PHP program, all the possible controllers get instantiated in IOC container, get their dependencies injected, but since requests usually need just 1 controller to be processed, all the other controllers and components that got created on startup of app and IOC container, they got just created for nothing, got never used, and then app just died without doing anything with those unused controllers/components, except the 1 controller that was matched by the route of the request ?

caribbean
  • 198
  • 1
  • 2
  • 12
  • By default spring managed objects are eagerly created singletons. More about changing the scope (example singleton or request) at [1] and eager or lazy answer at [2]. [1] https://www.baeldung.com/spring-bean-scopes. [2] https://stackoverflow.com/a/15092964/6522707 – Daniel F Aug 08 '20 at 15:02

1 Answers1

1

To answer your PHP question:

If a container is done right, it should instantiate ONLY the classes you use during the current request. If you use a single controller - it should make a single instance of that controller and inject all dependencies into it.

If a container makes instances of all possible classes that can be loaded through it - it will be extremely inefficient.

Usually the Containers keeps track of HOW to instantiate every possible instance that you might need. Like a config file. Graph of dependencies of every class that can be loaded from the container. etc.

That means the container KNOWS how to instantiate every class configurable through it, but it instantiates only the ones you use in the current request.

Also it should load only the files needed by the current request. Not all of them. However, if you use OPCache extension, they will not be parsed every time, but will be kept in a compiled variant in-memory of the PHP-FPM process.

vuryss
  • 1,270
  • 8
  • 16