Shibbir
Shibbir

Reputation: 2031

Cannot declare class Error, because the name is already in use

My index.php file container this code:

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

and libs/Bootstrap.php file contain this code:

<?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 page

<?php 

class Error {

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

Now when access this URL: http://localhost/practice/mvc/notexist then its' showing an error message: \

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

can you tell me how can I solve it?

Upvotes: 0

Views: 4322

Answers (1)

failedCoder
failedCoder

Reputation: 1424

Error class is one of the predefined classes in PHP, so you can't create your class with the name Error

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

Upvotes: 3

Related Questions