【问题标题】:Symfony 2 Translator object error in Php7Php7 中的 Symfony 2 Translator 对象错误
【发布时间】:2017-10-10 08:32:10
【问题描述】:

此代码之前在 php 5.4 中工作,但升级到 php7 后出现此错误:

类型错误:传递给 dell\Bundle\dellbundles\ot\otdell::__construct() 的参数 6 必须是 Symfony\Bundle\FrameworkBundle\Translation\Translator 的实例,给定 Symfony\Component\Translation\DataCollectorTranslator 的实例,在 /var/www/dellprj/vendor/dfolder/dfolderbundles/dell/Bundle/otde/Controller/otdellController.php 中调用

otdellController.php 的代码:

 $translator = $this->get('translator');

 $myvar = new OtDell($this, $this->container, $viewParameters, $logger, $request, $translator);

表示$translator 是问题所在。无论如何,这是 OtDell 类的代码:

 public function __construct(Controller $controller, ContainerInterface $container, array $viewParameters, LoggerInterface $logger, Request $request, Translator $translator)
    {......}

这里是 $translator 变量的 var_dump:

提前致谢。

【问题讨论】:

  • 在你的构造函数中,使用TranslatorInterface $translator而不是Translator $translator
  • 感谢您的评论,但同样的问题。
  • 我忘记导入(使用)TranslatorInterface 所以我添加了:使用 Symfony\Component\Translation\TranslatorInterface;

标签: php symfony


【解决方案1】:

导入翻译器界面: 使用 Symfony\Component\Translation\TranslatorInterface; 然后在构造函数中,使用 TranslatorInterface $translator 而不是 Translator $translator。

感谢@Thomas Mauduit-Blin

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多