2017-04-09 19 views
0

psr-4 및 종속성 주입 컨테이너 autoload 같은 PHP에서 mvc 아키텍처의 일부 고급 개념을 이해하려고합니다. 나는 의존성 주입 컨테이너를 만들고 작성자 autoload 사용하여 클래스를로드합니다. 클래스를 찾을 수 없습니다 : 의존성을 주입하는 재귀 반사

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

이 문제 뒤에 이유를 찾을 수 없습니다 컨트롤러 directory.I에서 수업을하지 않습니다 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(); 

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" 

      } 

    } 

} 
+0

여기에 composer.json를 넣어 –

+0

@ AminAlizade는 그것을 유감스럽게 생각합니다! composer.json이 게시물에 추가됩니다. –

답변

1

변경 라인 :

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

로는 :

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

작동하는 것처럼 보입니다! 그런 식으로 작동하는 이유를 설명해 주시겠습니까? 저는 이미 Controller \ Test의 네임 스페이스를 사용하고 있습니다. 컨테이너에 네임 스페이스를 전달해야하는 이유는 무엇입니까? –

+0

네, Container는 Controller 네임 스페이스를 알고 있습니다.하지만 ReflectionClass는 매개 변수로 $ class를 전달할 때 네임 스페이스를 알지 못한다고 생각합니다. 그리고 ReflectionClass에는'use Controller \ Test'와 같은 선언이 없습니다 .... – hcheung

+0

ok! Test 클래스의 생성자에서 Test2 클래스가 완성됩니다. 'Test2'가 $ class 변수에 전달되면 이번에 처음으로했던 것처럼 네임 스페이스를 통과하지 못합니다. 그러면 Ref2 클래스가 Test2 클래스를 어떻게 해결할 수 있을까요? –