class 未找到:递归反射以注入依赖项

class not found : recursive reflection to inject dependency

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

Fatal error: Uncaught exception 'ReflectionException' with message 'Class Test does not exist' in C:\xampp\htdocs\practice\reflection\Container\Container.php:15 Stack trace: #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} thrown in C:\xampp\htdocs\practice\reflection\Container\Container.php

似乎 Container.php 没有从控制器获得 类 directory.I 无法找出这个问题背后的原因。

我的目录结构如下:

-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();

Container.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 !';
    }
}

Composer.json :

{
    "autoload":{

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

            }

    }

}

换行:

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

至:

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