title: Creating objects dynamically in PHP
body: <p>In <code>index.php</code> I'm calling <code>Engine::router()</code> method (located in <code>app/Engine.php</code>) to get from request a string, for instance <code>'home'</code> and put them to <code>$controller</code>. Then I want to create new object using this string.</p>
  
  <pre><code>$className = $controler.'Controller';
  </code></pre>
  
  <p>Then i'm trying to create a object from class names <code>homeController</code>, located in <code>controllers/homeController.php</code> directory.</p>
  
  <pre><code>require 'controllers/'.$className.'.php';
  $CONTROLLER = new $className();
  </code></pre>
  
  <p>but i still get <code>Fatal error: Class 'homeController' not found in C:\xampp\htdocs\w8\app\Engine.php on line 67</code></p>
  
  <p><strong>Important!</strong> When I replace <code>$className</code> by 'homeController' (not a string) then object is created properly.</p>
  
  <pre><code>$CONTROLLER = new homeController();
  </code></pre>
  
  <p>Full code of app/Engine.php (class Engine)</p>
  
  <pre><code>public static function router()
      {
          $request = $_SERVER['REQUEST_URI'];
          $path = ltrim($_SERVER['REQUEST_URI'], '/');
          $requestElements = explode('/', $path);
  
          $requestController = $requestElements[0];
          $availableControllers = self::getControllers();
  
          if (self::checkController($requestController, $availableControllers))
          {
              $className = $requestController.'Controller';
              require 'controllers/'.$className.'.php';
              $CONTROLLER = new $className();
          }
      }
  </code></pre>
  
  <p>I'm using namespaces following:</p>
  
  <p><strong>root:</strong>
  <code>index.php (use app\Engine;)</code></p>
  
  <p><strong>directory app:</strong>
  <code>Engine.php (namespace app; use controllers\homeController;)</code></p>
  
  <p><strong>directory controllers:</strong>
  <code>homeController.php (namespace controllers;)</code></p>
  
  <p>Additional question: why when I'm unsing in Engine.php <code>use controllers;</code> instead <code>use controllers\homeController;</code> then I'm getting <code>Fatal error: Class 'app\homeController' not found in C:\xampp\htdocs\w8\app\Engine.php on line 67</code></p>
  
  <p>Line 67 is <code>$CONTROLLER = new $className();</code> or <code>$CONTROLLER = new homeController();</code> for both cases.</p>