关于依赖注入有个小问题

zhuyujin001

问题描述

这里写问题描述

代码:
 dump(Container::make(Response::class,
            [
                200,
                ['Content-Type' => 'application/json'],
                json_encode(['code'=>0,'msg'=>'ok'],JSON_UNESCAPED_UNICODE)
            ] )); 
打印出来的值
^ support\Response^ {#73
  #exception: null
  #_header: []
  #_status: 200
  #_reason: null
  #_version: "1.1"
  #_body: ""
  +file: null
}

为什么打印不出来header 'Content-Type' => 'application/json'以及body的值啊?是不是我用错了?

353 1 0
1个回答

Tinywan

看你是使用的哪个Container

  • zhuyujin001 2023-10-26

    照着手册安装的 composer require psr/container ^1.1.1 php-di/php-di ^6 doctrine/annotations ^1.14
    use support\Container;

  • Tinywan 2023-10-26

    你这种用法话没见过,配置文件配置了没,不知道你要干啥

  • zhuyujin001 2023-10-26

    配置按照手册配置好了
    $builder = new \DI\ContainerBuilder();
    $builder->addDefinitions(config('dependence', []));
    $builder->useAutowiring(true);
    $builder->useAnnotations(true);
    return $builder->build();
    然后根据手册使用// Container创建的实例可以依赖注入
    $user_service = Container::get(UserService::class);
    // Container创建的实例可以依赖注入
    $log_service = Container::make(LogService::class, [$path, $name]);

  • zhuyujin001 2023-10-26

    是不是我使用不对?

🔝