title: Symfony 4: DependencyInjection Component - Uncaught ReflectionException: Class does not exist
body: <p>I'm trying to implement the <a href="https://symfony.com/doc/current/components/dependency_injection.html" rel="nofollow noreferrer">DependencyInjection Component</a> from Symfony (version 4.0.0) into my project. For this I followed the following simple steps, in order to understand the <a href="https://symfony.com/doc/current/service_container/autowiring.html" rel="nofollow noreferrer">autowiring</a> process:</p>
  
  <ul>
  <li>In <code>composer.json</code>: Assign namespace <code>App</code> to the <code>src</code> folder.</li>
  <li>In <code>services.yaml</code>: Assign namespace <code>App</code> to the <code>src</code> folder.</li>
  <li>Define <code>MyController</code> class in <code>src</code> folder, under the namespace <code>App\Controller</code>.</li>
  <li>In <code>bootstrap.php</code>: Create a <code>ContainerBuilder</code> instance (<code>$container</code>).</li>
  <li>In <code>bootstrap.php</code>: Create a <code>YamlFileLoader</code> object and load the config file <code>services.yaml</code> into it.</li>
  <li>In <code>bootstrap.php</code>: Get an instance of <code>MyController</code> from the <code>$container</code> and display it on screen.</li>
  </ul>
  
  <p>But I keep receiving the following error:</p>
  
  <pre><code>( ! ) Fatal error: Uncaught ReflectionException: Class does not exist in &lt;path-to-my-project-root&gt;/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1051
  ( ! ) ReflectionException: Class does not exist in &lt;path-to-my-project-root&gt;/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1051
  Call Stack
  #   Time    Memory  Function    Location
  1   0.0160  354632  {main}( )   .../index.php:0
  2   0.0166  357928  require_once( '&lt;path-to-my-project-root&gt;/bootstrap.php' ) .../index.php:7
  3   0.0872  1752040 Symfony\Component\DependencyInjection\ContainerBuilder-&gt;get( )  .../bootstrap.php:16
  4   0.0872  1752040 Symfony\Component\DependencyInjection\ContainerBuilder-&gt;doGet( )    .../ContainerBuilder.php:522
  5   0.0872  1752816 Symfony\Component\DependencyInjection\ContainerBuilder-&gt;createService( )    .../ContainerBuilder.php:555
  6   0.0873  1752928 __construct ( ) .../ContainerBuilder.php:1051
  </code></pre>
  
  <p>The error occurs at this line (1051) in <code>ContainerBulder::createService</code> method, because <code>$definition-&gt;getClass()</code> returns <code>NULL</code>:</p>
  
  <pre><code>private function createService(Definition $definition, array &amp;$inlineServices, $id = null, $tryProxy = true) {
      // Line 1051:    
      $r = new \ReflectionClass($class = $parameterBag-&gt;resolveValue($definition-&gt;getClass()));
  
      //..
  }
  </code></pre>
  
  <p>In the chapter <a href="https://symfony.com/doc/current/service_container.html#service-container-services-load-example" rel="nofollow noreferrer">Automatic Service Loading in services.yaml</a>, as I understood it, by using only those settings without any other settings in <code>services.yaml</code>, the DI container will know how to create an instance of <code>MyController</code>. Maybe I'm wrong?...</p>
  
  <p>Could you please take a second to help me? Thank you very much.</p>
  
  <hr/>
  
  <p>My project consists of the following structure and files:</p>
  
  <h3>Project structure</h3>
  
  <pre><code>bootstrap.php
  composer.json
  config/
      services.yaml
  src/
      Controller/
          MyController
  vendor/
      symfony/
          config/
          dependency-injection/
          filesystem/
          polyfill-mbstring/
          yaml/
  </code></pre>
  
  <h3>composer.json</h3>
  
  <pre><code>"require": {
      "php": "&gt;=5.5.0",
      "symfony/dependency-injection": "^4.0",
      "symfony/config": "^4.0",
      "symfony/yaml": "^4.0",
  },
  "autoload": {
      "psr-4": {
          "App\\": "src/"
      }
  }
  </code></pre>
  
  <h3>bootstrap.php</h3>
  
  <pre><code>&lt;?php
  
  use App\Controller\MyController;
  use Symfony\Component\Config\FileLocator;
  use Symfony\Component\DependencyInjection\ContainerBuilder;
  use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  
  require '../vendor/autoload.php';
  
  $container = new ContainerBuilder();
  
  $fileLocator = new FileLocator(__DIR__ . '/config');
  $loader = new YamlFileLoader($container, $fileLocator);
  $loader-&gt;load('services.yaml');
  
  $myController = $container-&gt;get(MyController::class);
  
  var_dump($myController);
  </code></pre>
  
  <h3>config/services.yaml</h3>
  
  <pre><code>services:
      # default configuration for services in *this* file
      _defaults:
          autowire: true      # Automatically injects dependencies in your services.
          autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
          public: false       # Allows optimizing the container by removing unused services; this also means
                              # fetching services directly from the container via $container-&gt;get() won't work.
                              # The best practice is to be explicit about your dependencies anyway.
  
      # makes classes in src/ available to be used as services
      # this creates a service per class whose id is the fully-qualified class name
      App\:
          resource: '../src/*'
          exclude: '../src/{Entity,Migrations,Tests}'
  </code></pre>
  
  <h3>src/Controller/MyController</h3>
  
  <pre><code>&lt;?php
  
  namespace App\Controller;
  
  class MyController {
  
      public function myAction() {
          echo 'Hello from MyController.';
      }
  
  }
  </code></pre>
  
  <hr/>
  
  <h2>Update 1:</h2>
  
  <p>After container compilation with <code>compile()</code></p>
  
  <pre><code>$loader-&gt;load('services.yaml');
  $container-&gt;compile();
  </code></pre>
  
  <p>I received the following error:</p>
  
  <pre><code>( ! ) Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "App\Controller\MyController". in &lt;my-project-path&gt;/vendor/symfony/dependency-injection/ContainerBuilder.php on line 950
  ( ! ) Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "App\Controller\MyController". in &lt;my-project-path&gt;/vendor/symfony/dependency-injection/ContainerBuilder.php on line 950
  Call Stack
  #   Time    Memory  Function    Location
  1   0.1512  357648  {main}( )   .../index.php:0
  2   0.1675  361056  require_once( '&lt;my-project-path&gt;/bootstrap.php' )   .../index.php:7
  3   3.5621  2582624 Symfony\Component\DependencyInjection\ContainerBuilder-&gt;get( ???, ??? ) .../bootstrap.php:18
  4   3.5621  2582624 Symfony\Component\DependencyInjection\ContainerBuilder-&gt;doGet( ???, ???, ??? )  .../ContainerBuilder.php:522
  </code></pre>
  
  <p>I checked the <code>$container-&gt;definitions</code> array following the <code>compile()</code> call. I realised that all services (including <code>App\Controller\MyController</code>) saved in definitions list prior to <code>compile()</code> where removed from the array by the compilation process.</p>
  
  <p>More of it: In <code>$compiler-&gt;passConfig-&gt;log</code> I found these entries (after compilation step):</p>
  
  <pre><code>[0] string  "Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass: Removed service "Psr\Container\ContainerInterface"; reason: private alias."
  [1] string  "Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass: Removed service "Symfony\Component\DependencyInjection\ContainerInterface"; reason: private alias."
  [2] string  "Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass: Inlined service "App\Service\MyService" to "App\Controller\MyController"."
  [3] string  "Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass: Removed service "App\Controller\MyController"; reason: unused."
  [4] string  "Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass: Removed service "App\Service\MyService"; reason: unused."
  </code></pre>