Using an IoC container as a Service Locator is an Anti-Pattern.
You should prefer constructor injection over method injection or property injection:
If the class cannot do its job without the dependency, then add it to the constructor. The class needs the new dependency, so you want your change to break things. Also, creating a class that is not fully initialized ("two-step construction") is an anti-pattern (IMHO). If the class can work without the dependency, a setter is fine. - Source
Your IoC container should only be accessed one time for a given object tree. When I say accessed
I mean invoking DI::get('X')
. You objects should not invoke DI::get('X')
. Your objects should not be aware of the existence of an IoC container.
The IoC container should only be accessed from the Composition Root of your object graph.
In a MVC web application the composition root is usually a controller factory. In a console application it can be found in main.