Php 找不到类

Php 找不到类,php,include,Php,Include,我正在尝试用PHP编写一个前端控制器 这是我的密码: <?php require_once('Controller/LoginController.php'); require_once('View/LoginView.php'); require_once('Model/UserModel.php'); class FrontController { private $controller; private $view; public function __

我正在尝试用PHP编写一个前端控制器

这是我的密码:

<?php

require_once('Controller/LoginController.php');
require_once('View/LoginView.php');
require_once('Model/UserModel.php');

class FrontController
{
    private $controller;
    private $view;

    public function __construct(Router $router, $routeName, $action = null)
    {
        $route = $router->getRoute($routeName);

        $modelName = $route->model;
        $controllerName = $route->controller;
        $viewName = $route->view;

        $model = new $modelName;
        $this->controller = new $controllerName($model);
        $this->view = new $viewName($routeName, $model);

        if (!empty($action)) $this->controller->{$action}();
    }

    public function output() {

        if (!empty($this->view))
        {
            return $this->view->output();
        }
    }

}
PHP告诉我无法找到class
UserModel

但是,如果我将上述内容替换为静态表达式:

 $model = new UserModel();
 $this->controller = new LoginController();
 $this->view = new LoginView();
代码运行得很好,这可能告诉我这些类可以在我的代码中使用

我迷路了。有什么我忽略的吗

进一步查询此代码

if (!class_exists("UserModel")) die("No UserModel");
告诉我这个类不存在。那么,我怎样才能用
newusermodel()
构建它,以及如何修复它呢?

也许你可以检查一下

class_exists("UserModel");
如果返回false, 使用下面的代码获取所需的类名,检查它

get_declared_classes();
--编辑--

对不起,我误解了你的意思

以下代码

route-> model = new 'UserModel';
route-> view = new 'LoginView';
route-> controller = new 'LoginController';

必须为存储在变量中的类名指定名称空间,因为它们是在运行时解析的:

$class = 'Your\Full\Namespace\Path\UserModel';
// ...
$object = new $class;
PHP无法可靠地检测动态类名所隐含的名称空间


此外,随着项目的发展,跟踪依赖关系变得越来越困难。我建议实施一个系统,或者使用现有的解决方案,例如。

我认为is应该是
$model=new$modelName()添加parentheses@mseifert当前位置不,不是这样。我已经这样做了,它告诉我课程不可用。但是,当我按它的名字称呼它时,它被正确地包含并可供使用。
$class = 'Your\Full\Namespace\Path\UserModel';
// ...
$object = new $class;