So I know the basics of Dependency Injection. I should pass in my dependencies as method parameters instead of instantiating the objects themselves inside the method.
I was wondering if this also applies to classes that come with PHP, such as DateTime objects or ReflectionClass objects?
For example one of the methods of a class I’m working on needs to know about the properties of a object that is passed in. I wanted to use a ReflectionClass to accomplish the task, but I thought it seemed unnecessary to have to pass a ReflectionClass object as a parameter if the class will always be accessible and relatively unchanged (Unless ReflectionClass changes with newer PHP versions).
It depends on the design requirements of your application.
If you know that your application requires a DateTime for a specific method to function, then pass in a DateTime. If you require custom functionality around a DateTime, then consider sub-classing DateTime and send that.
If you decide to use PHP’s type hints in your method signature, consider using an interface instead, so that any object sent will fulfill that contract with the receiving class. This approach can also help insulate your application against incompatible changes in PHP in the future.