如何使用PHP实现工厂类

如何使用PHP实现工厂类

本文介绍了如何使用PHP实现工厂类-依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将以下代码作为我想要的示例:

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's 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:
        // ----------------------
    }
}

我想实现工厂类。


  • 此工厂类将由控制器类扩展。

  • 工厂类将查看构造参数控制器类,并根据工厂中的AddBindings方法创建对象的新实例。

假设我想拥有一个具有来自MySQL的数据的MySQLSomethingRepository ...注入到我的SomethingController中...在某个地方我需要声明

Let's say I wanted to have a MySQLSomethingRepository which has data coming from MySQL... injected into my SomethingController... Somewhere I need to declare that

SomethingController( new MySQLSomethingRepository() )...

希望可以由我的工厂类处理...

which hopefully will be dealt with by my factory class...

我目前的操作方式是强制与数据源直接耦合……这使得很难用无线方式进行测试用例th:

The current way i'm doing it is that is forcing a direct coupling with the data source... which is making it very hard to do test cases with:

private $somethingRepository = new MySQLSomethingRepository();

所以想像一下,如果我在其他控制器的负载中使用了相同的存储库,并且想更改数据库源到一些json数据,然后实现以下存储库 JsonSomethingRepository,我必须去将所有控制器更改为:

so imagine if i have used this same repository in loads of other controllers and i want to change my database source to some json data and i implement the following repository "JsonSomethingRepository", I have to go and change all of the controllers to:

private $somethingRepository = new JsonSomethingRepository();

我如何实现我的Factory类,以便它可以处理创建控制器类要求的实例

How might i implement my Factory class so that it can deal with creating the instances my controller class is demanding inside the AddBindings function?

推荐答案

在Adapter模型中设计一个抽象类,并为子类提供一些通用方法。
您可以设计两个带有适配器的存储库,以将它们注入到控制器中。

Design an abstract class in Adapter model and provide some common methods for child class.You can design both repos with adapters to injected in you controllers.

我的建议是使用Abstract类并按以下方式进行操作:

My recommendtation is to use Abstract class and do it in way below:

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's construct)
    }
}

希望帮忙。

这篇关于如何使用PHP实现工厂类-依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:51