将一个类注入另一个类

时间:2014-09-19 20:18:56

标签: php dependency-injection laravel-5

我有一个 InjectedClass 类,方法是 someMethod 。这节课有三个参数。我需要在另一个类 Myclass 中输出方法。基本上,我需要通过 Myclass 中的构造函数将相同的三个参数传递给注入的类,它会返回一些数据。

问题是我得到一个错误说明,Myclass构造函数中的第四个参数(注入的类)应该是injectClass的一个实例,但它是空的!参见示例。

我认为问题在于Injected类正在初始化而没有params,因此没有初始化,因此是空的。此外,如果我尝试将类直接注入 myfunction 方法,我会得到或多或少相同的结果。我该如何解决这个烂摊子?

class Myclass {

    private $var1;
    private $var2;
    private $var3;
    private $injectedclass;

    public function __construct($var1, $var2, $var3, InjectedClass $injectedclass)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
        $this->injectedclass = $injectedclass;
    }

    public function myfunction()
    {
        return $this->injectedclass($this->var1, $this->var2, $this->var3)->someMethod();
    }
}

class InjectedClass {

    private $var1;
    private $var2;
    private $var3;

    public function __construct($var1, $var2, $var3)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
    }

    public function someMethod()
    {
        // do stuff
    }
}

3 个答案:

答案 0 :(得分:0)

MyClass在构造中只需要1个参数--InjectedClass instansce,它可以工作了。即。

$injected = new Injected($var1, $var2, $var);

$my = new MyClass($injected);

$my->myfunction(); // will work correctly now

答案 1 :(得分:0)

我会重新考虑你是如何尝试使用依赖注入的。最简单的方法是在InjectedClass中创建setter,并在构造中设置类变量时设置它们。类似的东西:

class Myclass {

    private $var1;
    private $var2;
    private $var3;
    private $injectedclass;

    public function __construct($var1, $var2, $var3, InjectedClass $injectedclass)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
        $this->injectedclass = $injectedclass;
        $this->injectedclass->setParams($this->var1, $this->var2, $this->var3);
    }

    public function myfunction()
    {
        return $this->injectedclass($this->var1, $this->var2, $this->var3)->someMethod();
    }
}

class InjectedClass {

    private $var1;
    private $var2;
    private $var3;

    public function setParams($var1, $var2, $var3) {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
    }

    public function someMethod()
    {
        // do stuff
    }
}

当然你可以稍微清理一下,但通过这种方式注射你应该能够避免你所面临的问题。

答案 2 :(得分:0)

一切都取决于你想要达到的目标,但基本上都在

public function myfunction()
{
   return $this->injectedclass($this->var1, $this->var2, $this->var3)->someMethod();
}

您不需要传递这些参数,因为$this->injectedclass已经是已经初始化其属性的对象。所以代码应该是这样的:

class Myclass {

    private $var1;
    private $var2;
    private $var3;
    private $injectedclass;

    public function __construct($var1, $var2, $var3, InjectedClass $injectedclass)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
        $this->injectedclass = $injectedclass;
    }

    public function myfunction()
    {
        return $this->injectedclass->someMethod();
    }
}

class InjectedClass {

    private $var1;
    private $var2;
    private $var3;

    public function __construct($var1, $var2, $var3)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
    }

    public function someMethod()
    {
        // do stuff
        echo $this->var1.' '.$this->var2.' '.$this->var3."<br />";
    }
}

$m = new Myclass(1,2,3, new InjectedClass(1,2,3));
$m->myFunction();

但是,当您看到创建Myclass实例时,您需要将参数1,2,3传递给MyclassInjectedClass,如果这些值相同,则可能不是非常方便。

因此,您可以将代码更改为:

class Myclass {

    private $var1;
    private $var2;
    private $var3;
    private $injectedclass;

    public function __construct($var1, $var2, $var3, InjectedClass $injectedclass)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
        $this->injectedclass = $injectedclass;
        $this->injectedclass->setParams($var1, $var2, $var3);
    }

    public function myfunction()
    {
        return $this->injectedclass->someMethod();
    }
}

class InjectedClass {

    private $var1;
    private $var2;
    private $var3;

    public function setParams($var1, $var2, $var3)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
    }

    public function someMethod()
    {
        // do stuff
        echo $this->var1.' '.$this->var2.' '.$this->var3."<br />";
    }
}

$m = new Myclass(1,2,3, new InjectedClass());
$m->myFunction();

所以你只将参数传递给Myclass并将参数设置为InjectedClass。但是这种方法会导致即使您创建InjectedClass类对象,也需要运行setParams方法来设置参数,因此它不是最佳解决方案。

更好的是:

class Myclass {

    private $var1;
    private $var2;
    private $var3;
    private $injectedclass;

    public function __construct(InjectedClass $injectedclass)
    {
        $this->injectedclass = $injectedclass;
        list ($this->var1, $this->var2, $this->var3) = $this->injectedclass->getParams();
    }

    public function myfunction()
    {
        return $this->injectedclass->someMethod();
    }
}

class InjectedClass {

    private $var1;
    private $var2;
    private $var3;

    public function __construct($var1, $var2, $var3)
    {
        $this->var1 = $var1;
        $this->var2 = $var2;
        $this->var3 = $var3;
    }

    public function getParams() {
        return array ($this->var1, $this->var2, $this->var3);
    }

    public function someMethod()
    {
        // do stuff
        echo $this->var1.' '.$this->var2.' '.$this->var3."<br />";
    }
}

$m = new Myclass(new InjectedClass(1,2,3));
$m->myFunction();

InjectedClass的对象可以将这些参数返回给Myclass的对象。