无法声明类错误,因为该名称已被使用

时间:2019-06-21 14:29:34

标签: php

我的index.php文件容器此代码:

<?php 
require 'libs/Bootstrap.php';
$app = new Bootstrap();

和libs / Bootstrap.php文件包含以下代码:

<?php 

class Bootstrap {

    public function __construct () {

        $url = $_GET['url'];
        $url = rtrim($url, '/');
        $url = explode('/', $url);

        // print_r($url);

        $file = 'controllers/'.$url[0].'.php';

        if(file_exists($file)) {
            require_once $file;
            echo 2;
        } else {            
            require_once 'controllers/error.php';
            $controller = new Error();          
            return false;
        }

        echo 3;

        $controller = new $url[0];

        if(isset($url[2])) {
            $controller->{$url[1]}($url[2]);    
        } else {
            if(isset($url[1])) {
                $controller->{$url[1]}();   
            }
        }
    }   


}

Error.php页面

<?php 

class Error {

    public function __construct () {
        echo 'from error class';
    }
}

现在访问此URL:http://localhost/practice/mvc/notexist时,它显示一条错误消息:\

Cannot declare class Error, because the name is already in use in D:\xampp\htdocs\practice\mvc\controllers\error.php on line 3

你能告诉我如何解决吗?

1 个答案:

答案 0 :(得分:3)

错误类是PHP中预定义的类之一,因此无法使用名称“错误”创建类

https://www.php.net/manual/en/class.error.php