Take the following code as an example of what I want:
class SomethingController extends Factory
{
private $somethingRepository;
public function __Construct( ISomethingRepository $repo )
{
$this->somethingRepository = $repo;
}
}
class Factory
{
public function __Construct()
{
$this->AddBinding( ISomethingRepository, MySQLSomethingRepository);
}
public function AddBinding( $interface, $concrete )
{
$calledClass = get_called_class();
$class = new \ReflectionClass( $calledClass );
$method = $class->getMethod( "__construct" );
$params = $method->getParameters();
foreach( $params as $param )
{
if ( $param == $interface )
{
return new $concrete;
}
}
}
}
I want to implement a class of class factory.
- This factory class will be extended by the controller class.
- the factory class will examine the design parameters of the controller class and create new object instances based on my AddBindings method in the factory.
Let's say I wanted to have a MySQLSomethingRepository that has data coming from MySQL ... entered into my SomethingController ... Somewhere I need to declare that
SomethingController( new MySQLSomethingRepository() )...
which hopefully will be reviewed by my factory class ...
The current way I'm doing it is that it creates a direct link to the data source ... which makes it very difficult to run test cases:
private $somethingRepository = new MySQLSomethingRepository();
, , json-, "JsonSomethingRepository",
private $somethingRepository = new JsonSomethingRepository();
factory, , AddBindings?