-1

Though there is not any error while requiring the file. I have not found error while instantiating the class.

//$this->currentController = /var/www/html/RoomFinder/App/Controllers/Home.php
require $this->currentController;
//No any errors till here and note that file_exists returns true
$this->currentController = new $this->currentController;

Here is the error message:

Fatal error: Uncaught Error: Class '/var/www/html/RoomFinder/App/Controllers/Home.php' not found in /var/www/html/RoomFinder/Core/Router.php:29 Stack trace: #0 /var/www/html/RoomFinder/public/index.php(10): Router->__construct() #1 {main} thrown in /var/www/html/RoomFinder/Core/Router.php on line 29
Yojan
  • 159
  • 1
  • 10

2 Answers2

1

Try this:

require $this->currentController;
$this->currentController = new Home;

I think is better if you instantiate Home class in another var like this:

$home = new Home;
1

You're trying to instantiate a path, rather than an object.

 require $this->currentController; // is including a path to a php file.

Assuming your class within that path is called 'Home' try this:

 require $this->currentController;
 $this->currentController = new Home;

although, i'd suggest refactoring this code to be easier to understand:

 $controllerpath = $this->currentController;
 $controllername = 'Home';
 require $controllerpath;
 $this->currentController = new $controllername;
Matthew Knight
  • 631
  • 5
  • 13
  • Thank you very much. Would you point out to some resources to learn best practices to write better code? – Yojan Jun 06 '18 at 16:10