PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

[FIXED] class not found : recursive reflection to inject dependency

 January 22, 2022     autoload, composer-php, dependency-injection, php, psr-4     No comments   

Issue

I am trying to understand some advanced concept of mvc architecture in php,like autoload with psr-4 and dependency injection container.I made a dependency injection container and load classes using composer autoload. Whenever i run this code i get the following error :

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

Seems like Container.php doesn't get classes from controller directory.I could not find out the reason behind this problem.

My directory structure is as follows :

-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"

            }

    }

}

Solution

Change the line:

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

to:

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


Answered By - hcheung
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing