【问题标题】:class not found : recursive reflection to inject dependency找不到类:递归反射注入依赖
【发布时间】:2017-09-04 07:35:49
【问题描述】:

我正在尝试了解 php 中 mvc 架构的一些高级概念,例如使用 psr-4 自动加载和依赖注入容器。我制作了一个依赖注入容器并使用 composer autoload 加载类。 每当我运行此代码时,我都会收到以下错误:

致命错误:带有消息的未捕获异常“ReflectionException” '类测试不存在'在 C:\xampp\htdocs\practice\reflection\Container\Container.php:15 堆栈 跟踪:#0 C:\xampp\htdocs\practice\reflection\Container\Container.php(15): ReflectionClass->__construct('Test') #1 C:\xampp\htdocs\practice\reflection\index.php(9): Container\Container::newInstanceOf('Test') #2 {main} 投入 C:\xampp\htdocs\practice\reflection\Container\Container.php

好像 Container.php 没有从控制器目录中获取类。我找不到这个问题背后的原因。

我的目录结构如下:

-reflection Directory
  -Controller folder
    -Test.php
    -Test2.php
  -Container Directory
    -Container.php
-vendor Directory
-index.php

index.php:

require 'vendor/autoload.php';

use Container\Container;
use Controller\Test;
use Controller\Test2;

$test = Container::newInstanceOf('Test');

$test->testHi();

容器.php:

namespace Container;

class Container
{

     public static function newInstanceOf($class)
     {

         $reflection = new \ReflectionClass($class);

         $constructor = $reflection->getConstructor();

         if ( ! $constructor)
         {

             return new $class;

         }
         $params = $constructor->getParameters();

         if (count($params) === 0)
         {

             return new $class;

         }

         $newInstanceParams = [];

         foreach($params as $param)
         {
             if(is_null($param->getClass()))
             {
                 $newInstanceParam[] = null;

                 continue;
             }

             $newInstanceParams[] = self::newInstanceOf($param->getClass()->getName());

         }

         return $reflection->newInstanceArgs($newInstanceParams);

     }

}

Test.php:

namespace Controller;

class Test
{

    private $test2;

    public function __construct(Test2 $test2)
    {

        $this->test2 = $test2;

    }
    public function testHi()
    {
        $this->test2->test2Hi();

    }


}

Test2.php:

namespace Controller;

class Test2
{

    public function __construct()
    {

    }

    public function test2Hi()
    {
       echo 'hi from test 2 !';
    }
}

作曲家.json:

{
    "autoload":{

            "psr-4":{
                     "Controller\\" : "Controller",
                     "Container\\"  : "Container"

            }

    }

}

【问题讨论】:

  • 把你的 composer.json 放在这里
  • @AminAlizade 对此感到抱歉! composer.json 被添加到帖子中。

标签: php dependency-injection composer-php autoload psr-4


【解决方案1】:

换行:

$test = Container::newInstanceOf('Test');

到:

$test = Container::newInstanceOf('Controller\Test');

【讨论】:

  • 看起来它正在工作!你能解释一下为什么它会这样工作吗?我已经“使用 Controller\Test”namepsace。为什么我需要将命名空间传递给容器?
  • 是的,Container 知道 Controller 命名空间在哪里,但我认为 ReflectionClass 不知道参数 $class 在您作为参数传入时所在的命名空间。而且 ReflectionClass 没有像use Controller\Test....这样的声明。
  • 好的!从Test类的构造函数中,实现了Test2类。当'Test2'被传递给$class变量时,这次我们没有像第一次那样传递命名空间。那么反射类是如何解析Test2类的呢?
  • var_dump($newInstanceParams); 看看。
猜你喜欢
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 2011-10-10
  • 2018-10-22
  • 1970-01-01
相关资源
最近更新 更多