I have seen number of frameworks implement DI in php. However, since all objects in php get created and destroyed during the lifetime of a single request, I wonder how this affects performance.
Generally, you will define a number of objects inside a DI container. In php, even if a Controller does not need most of these objects, they will still be instantiated.
Also, if you declare your DI dependencies in a php file, you will load all referenced scripts. Without DI, you load only what you need.
I have seen some frameworks permit lazy DI, so this should help with unneeded instantiation. Maybe segmenting DI is also a way to address the first issue.
So, will DI adversely affect my php application performance and how should I go about implement DI in php so it does not happen?
You have to identify if there is a problem, and then what is the problem.
You can use lazy-injection, something like:
(example from PHP-DI)
You have to ask yourself if dependency injection is really the problem. If you where creating new instances (or using Singletons) instead of using DI, would the problem go away? (I don’t think so)
IMO performance considerations should come with a clear identification that DI is the problem. There are so many parts that can go wrong in an application.