Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

I don't understand why my machine running PHP 7.2.9 gives this error:

Fatal error: Cannot declare class Error, because the name is already in use in controllers\error.php on line 3

I have a file named controllers/error.php which contains:

<?php

class Error {

    function __construct() {
        echo 'Error: 404 not found the file.';
    }

}

Meanwhile, I have a file named /index.php which contains:

require "controllers/error.php";
$controller = new Error;

Even if I change from require to require_once "controllers/error.php", it still keeping reporting the same message.

like image 812
D.S. Avatar asked Dec 14 '22 14:12

D.S.


1 Answers

Error is a built-in class in PHP 7.

As such, you cannot make a class Error {}.

Rename the class, or put it in a namespace to avoid conflict.

(Or, as a third option, you could consider using/extending the built-in class instead of making your own error handling system from scratch.)

like image 167
ceejayoz Avatar answered Dec 18 '22 00:12

ceejayoz