How to implement a factory class using PHP - Enabling dependency

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()
    {
        // The following call to AddBinding would push into SomethingController the new instance of the class denoted in my AddBinding second parameter.
        $this->AddBinding( ISomethingRepository, MySQLSomethingRepository);
        // So in this case, if the controller i'm extending has a construct parameter of ISomethingRepository, then make the parameter equal a new MySQLSomethingRepository()
        // Then if I want to use JSONSomethingRepository in the future, I only have to change the one AddBinding call and the controller will still work.
    }

    public function AddBinding( $interface, $concrete )
    {
        // Somehow assign the constructor properties of the extending class as new instances of the bindings i have called upon in the construct of my factory class (see this class construct)
        // Pseudo code:
        // ----------------------
        $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;
            }
        }
        // /Pseudo code:
        // ----------------------
    }
}  

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?

+2
1

. .

- :

class SomethingController extends AbstractController {
}

abstract class AbstractController {
    protected $somethingRepository;
    public function __Construct(ISomethingRepository $repo) {
        $this->somethingRepository = $repo;
        $this->AddBinding ( ISomethingRepository, MySQLSomethingRepository );
    }
    public function AddBinding($interface, $concrete) {
        // Somehow assign the constructor properties of the extending class as new instances of the bindings i have called upon in the construct of my factory class (see this class construct)
    }
}

, .

0

All Articles