I am writing a PHP application with the ability to test, so my classes always ask their designers about the "co-authoring objects" on which they depend on the Injection Dependency pattern.
That way I can pass in mocks or test implementations in my unit tests.
What I want to achieve is to create an instance of the class with null values passed to its constructor arguments in order to call the return mechanism, which creates an instance of the default implementation class for each collaborator.
Since object type parameters cannot be set by default in PHP, I have to do this inside the constructor. The following code is an example of the approach I am currently using:
class Engine
{
private $loader;
private $logger;
public function __construct(ResourceLoader $loader = null, Logger $logger = null)
{
if ($loader == null) $loader = new DefaultResourceLoader;
if ($logger == null) $logger = new DefaultLogger;
$this->loader = $loader;
$this->logger = $logger;
}
}
? IoC ?