如何调用以接口为参数的方法? Symfony-PHP

时间:2018-08-06 10:52:25

标签: php symfony

我感觉很愚蠢,但是我不知道如何调用以接口为参数的函数。因此,我在课堂上添加了一个方法:

public function sendSpool($messages = 10, KernelInterface $kernel)
{
    $application = new Application($kernel);
    $application->setAutoExit(false);

    $input = new ArrayInput(array(
       'command' => 'swiftmailer:spool:send',));

    $output = new BufferedOutput();
    $application->run($input, $output);

    $content = $output->fetch();

    return new Response($content);
}

我怎么从控制器中调用它?

我尝试过:

$this->sendSpool('test', KernelInterface::class);

然后:

$kernel = new HttpKernel();
$this->sendSpool('test', $kernel );

此内核界面在我的使用声明中

use Symfony\Component\HttpKernel\KernelInterface;

,但是我不知道如何通过,如果有人花了几分钟,请帮助我解释一下。谢谢。

1 个答案:

答案 0 :(得分:5)

您需要使用实现该接口的类的实例。基本上,它是在要求您提供一个类,该类具有如界面中所述的可用功能。

class MyKernel implements KernelInterface { /* functions here */ }
class x {
  function x() {
    $obj = new MyKernel();
    $this->sendSpool('test', $obj);
  }
}

在Symfony中,通常有一种获取内核的方法。像这样:

$this->getApplication()->getKernel()

但这取决于您的用例。

此外,您需要在实现该接口的类中使用use语句,而在使用它的地方不需要它。