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

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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题