phpredeclare

PHP5.5: Fatal error: Cannot redeclare class


since I upgraded from PHP 5.2 to 5.5 I get an error I don't understand by now.

Fatal error: Cannot redeclare class sessionHandler in ... on line ...

Well before updating the PHP version this error didn't raise and redeclaring a class should be an error independent from PHP version as I guess. Further I always use require_once() which should help to avoid a mistake on that.

So just to make sure it doesn't be redeclared, I added a backtrace code block before that class declaration. So hopefully, I thought it would output twice, but I get only one backtrace output at all. Therefore it gets declared only once from my point of few.

Do I miss something? Any idea how to find the "real" issue?


Solution

  • Class "SessionHandler" already exists in the namespace as it's a class in PHP - http://php.net/manual/en/class.sessionhandler.php

    Looks like the class was included in PHP 5.4 so it explains everything.

    Try to think of some other name for the class or define a namespace. If you create a namespace, something like..

    namespace App;
    class sessionHandler {
    ....
    

    you won't get the error anymore but you will need to use App\sessionHandler whenever you're referring to your own class.