Php 实例化类时未找到类文件

Php 实例化类时未找到类文件,php,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 = /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
试试这个:

require $this->currentController;
$this->currentController = new Home;
我认为最好在另一个变量中实例化Home类,如下所示:

$home = new Home;

您试图实例化一条路径,而不是一个对象

 require $this->currentController; // is including a path to a php file.
假设您在该路径中的类称为“Home” 试试这个:

 require $this->currentController;
 $this->currentController = new Home;
尽管如此,我还是建议重构此代码以便更容易理解:

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

@praveen idk为什么你将它标记为重复的,尽管它显然不是语法错误。嗨,Yojan,我相信这已经包含在其中一个答案中了。让我知道如果它没有涵盖,我很高兴重新开放<代码>:)@praveen请求后没有错误。该错误发生在实例化该类之后。那么这一行有语法错误吗?另外,你提供的链接中没有提到。疯狂的一个。。。好的,我还没看到这个问题。重新开放。最好的办法是找到解决方案。。。让我也知道<代码>:)该文件中的实际类名是什么?您需要将
new
与类名而不是文件名一起使用。非常感谢。您能指出一些资源来学习编写更好代码的最佳实践吗?