实例化类时未找到类文件

时间:2018-06-06 15:25:48

标签: php

虽然在需要文件时没有任何错误。我在实例化类时没有发现错误。

//$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;

以下是错误消息:

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

2 个答案:

答案 0 :(得分:1)

试试这个:

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

我认为如果你在另一个var中实例化Home类会更好:

$home = new Home;

答案 1 :(得分:1)

您正在尝试实例化路径,而不是对象。

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

假设您在该路径中的课程称为“家庭” 试试这个:

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

虽然,我建议重构这段代码以便更容易理解:

 $controllerpath = $this->currentController;
 $controllername = 'Home';
 require $controllerpath;
 $this->currentController = new $controllername;