douqihou7537 2019-01-14 14:13
浏览 246
已采纳

Symfony DI:Uncaught ArgumentCountError:函数App :: __ construct()的参数太少,0在第28行的index.php中传递,正好是1

I'm trying to implement Symfonys dependency injection container.

I have 2 containers set, one for the database, and one for the system user.

and I'm using "addArgument()" to both the App class and SystemUser class, pushing to App class a SystemUser object, and for the SystemUser class a Database object.

index.php:

require_once 'vendor/autoload.php';

use TestingDI\App;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

$containerBuilder = new ContainerBuilder();
$containerBuilder->register('database', '\TestingDI\Database');

$containerBuilder->register('system.user', '\TestingDI\SystemUser')
                 ->addArgument(new Reference('database'));

$containerBuilder->register('app', '\TestingDI\App')
                 ->addArgument(new Reference('system.user'));


$database       = $containerBuilder->get('database');
$systemUser     = $containerBuilder->get('system.user');
$app            = $containerBuilder->get('app');

# Initialize App class
$app = new App();

App.php:

<?php 

namespace TestingDI;

use TestingDI\SystemUser; 


class App {

    public $systemUser; 

    public function __construct(SystemUser $systemUser)
    {
        var_dump($systemUser);
    }
}

I do see my var_dump result, with the object, but keep getting this error:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function TestingDI\App::__construct(), 0 passed in /www/potato/symfony-di/index.php on line 28 and exactly 1 expected in /www/potato/symfony-di/testingdi/App.php:12

Stack trace:

0 /www/potato/symfony-di/index.php(28): TestingDI\App->__construct()

1 {main} thrown in /www/potato/symfony-di/testingdi/App.php on line 12

These are my other classes:

SystemUser.php

<?php 
namespace TestingDI;

use TestingDI\Database;

class SystemUser {

    public $db; 

    public function __construct( Database $database )
    {
        $this->db = $database;
    }
}

Database.php

<?php 
namespace TestingDI;

class Database {

    public function __construct()
    {

    }
}
  • 写回答

1条回答 默认 最新

  • dongxuxian1123 2019-01-14 14:38
    关注

    There seems to be a basic misconception on what dependency injection is and how to use it.

    Since you are using a dependency container to build the different parts of your app, you should be getting those objects from the container, not instantiate them directly.

    And, if you are going to be instantiating them directly (e.g. new App()) you need to respect the regular language rules. In this case, the constructor's signature:

    public function __construct(SystemUser $systemUser)
    

    If you call the constructor without passing it an object of the SystemUser class, it will fail, in the same way any other method/function will fail if you call it in a way that doesn't conform with its signature.

    By the time you did

    $app            = $containerBuilder->get('app');
    

    Your app was already "initialized", and you didn't need to call new at all.

    By doing this:

    $containerBuilder = new ContainerBuilder();
    $containerBuilder->register('database', '\TestingDI\Database');
    
    $containerBuilder->register('system.user', '\TestingDI\SystemUser')
                     ->addArgument(new Reference('database'));
    
    $containerBuilder->register('app', '\TestingDI\App')
                     ->addArgument(new Reference('system.user'));
    
    
    $database       = $containerBuilder->get('database');
    $systemUser     = $containerBuilder->get('system.user');
    $app            = $containerBuilder->get('app');
    

    You were doing the equivalent of:

    $database   = new Database();
    $systemUser = new SystemUser($database);
    $app        = new App($systemUser);
    

    Only you were letting the DI container do the work for you. Which of course, given how verbose your container definition is, seems kinda pointless.

    But you can let the power of autowiring do most of the heavy-lifting for you, and do the following:

    $container = new ContainerBuilder();
    $container->autowire( Database::class );
    $container->autowire( SystemUser::class );
    $container->autowire( App::class )
              ->setPublic( true );
    
    $container->compile();
    
    // this will get you an application instance with all the dependencies
    // resolved and injected in the class.
    $app = $container->get( App::class );
    

    I now notice you already had a similar problem while trying a different DI library, and I realize now that the whole problem revolves about a simple misunderstanding about what problem a DI container deals with

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?