Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 2依赖项注入“;必须是“的一个实例”;(构造函数可能没有传递任何内容。)_Symfony_Dependency Injection - Fatal编程技术网

Symfony 2依赖项注入“;必须是“的一个实例”;(构造函数可能没有传递任何内容。)

Symfony 2依赖项注入“;必须是“的一个实例”;(构造函数可能没有传递任何内容。),symfony,dependency-injection,Symfony,Dependency Injection,我目前正在尝试为我的包将一个类的依赖项注入到另一个类中。 我试图以Symfony2文档中关于时事通讯/邮件的示例为基础。 我当前遇到以下错误: Catchable Fatal Error: Argument 1 passed to Me\MyBundle\Resources\component\SecondClass::__construct() must be an instance of Me\MyBundle\Resources\component\FirstClass\FirstClas

我目前正在尝试为我的包将一个类的依赖项注入到另一个类中。 我试图以Symfony2文档中关于时事通讯/邮件的示例为基础。 我当前遇到以下错误:

Catchable Fatal Error:
Argument 1 passed to Me\MyBundle\Resources\component\SecondClass::__construct()
must be an instance of Me\MyBundle\Resources\component\FirstClass\FirstClass,
none given
MyBundle.php

namespace Me\MyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Resources\component\FirstClass;
use Resources\component\SecondClass;

class MeMyBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container
            ->register('first_class', 'FirstClass');

        $container
            ->register('second_class', 'SecondClass')
            ->addArgument(new Reference('first_class'));
    }
}
$container
    ->register('first_class', 'Me\MyBundle\Resources\component\FirstClass');

$container
    ->register('second_class', 'Me\MyBundle\Resources\component\SecondClass')
    ->addArgument(new Reference('first_class'));
/参考资料/component/FirstClass.php

<?php
namespace Me\MyBundle\Resources\component;

class FirstClass
{
    ....
}
SecondClass.php

<?php
namespace Me\MyBundle\Resources\component;

class SecondClass {    

    private $firstClass;

    public function __construct ( FirstClass\FirstClass $firstClass ) {
        $this->firstClass = $firstClass;
    }
    ....
public function __construct ( LanguageCodes $langCodes )
这主要是与名字空间的斗争

多亏了Cerad的耐心,我终于把它整理好了。

你为什么有这个:

$container = new ContainerBuilder(); ?
$container作为参数传递。我偷偷怀疑你不是用这个容器来拿你的东西的

你在什么地方这样做:

$instance = $this->container->get('second_class");
或者你正在尝试:

$instance = new SecondClass();

我已删除ContainerBuilder()行。谢谢你。是的,我在做$instance=newsecondclass();在我的控制器内。删除该行后,它现在确实会无误地加载页面。但是我猜当我试着给它打这样的电话时,它会出错。您需要使用容器来创建对象。从控制器内部,$this->get('second_class');如果我把这个添加到控制器:$sc=$this->container->get('second_class');它给出了这个错误:“在/public_html/app/cache/dev/appDevDebugProjectContainer.php行1291中找不到类'SecondClass'”我也尝试了$this->get('second_Class');但是它返回了相同的错误。行是这样做的:“new\SecondClass($this->get('first_class'));”因此我认为我在那里有一个名称空间问题。