Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
"Cannot redeclare class" issue
  • I'm try to work namespaces in a default FuelPHP installation I add the following to the welcome controller (otherwise unedited) and I start getting the error:

    ErrorException [ Compile Error ]: Cannot redeclare class Fuel\Controller\Welcome"


    The code is the default installation changing the namespace and extends:


    <?php
    namespace Fuel\Controller;

    use Fuel\Core\Controller;

    class Welcome extends Controller
    {
    ...

    }

    This is probably a beginner's question but I just can't figure out why the collision is occurring and I have tried everything I can think of.


    EDIT: I even tried putting the following code in front of the class but nothing changed:


    if (class_exists("Welcome",false)) {

        echo "here. (" . __FILE__ . ":" . __LINE__ . ")\n";

        die();

    } else {

    //Class definition...

    }

  • You usually get this when you have either a namespace issue or a case issue.

    The autoloader is case sensitive, so if you use two different ways to load the same file, you get this error when the autoloader tries to load the same file for the second time. It's a PHP compile time error, so you get this before anything is executed.

    From what I understand you simply added a namespace to the existing Welcome controller. I'm afraid it doesn't work that way.

    Fuel uses what is called a "cascading" file system, which means that the combination of namespace and class. And unless you have changed your configuration, all controllers MUST start with "Controller_"

    Please check http://docs.fuelphp.com/general/controllers/base.html to see how you should name your controllers, how namespacing works, and how that maps to files on disk.
  • Harro, Thanks for your answer. However is it not consistent with the document link you provided. According to the "namespacing controllers" section of the document, when a namespace was added the "Controller_" prefix to the class name is dropped. I tried it anyway but the error message was unchanged at "ErrorException [ Compile Error ]: Cannot redeclare class Controller\Controller_Welcome"

    As noted, I checked the cases but they seem correct. 

    The code I used is the below:

    <?php


    namespace Controller;

    use Fuel\Core\Controller as CoreController;

    class Controller_Welcome extends CoreController

    {
    ...

    }


  • That's not what it says. That section is about changing the "Controller_" prefix to "\Controller\" namespace.

    You are now mixing the two.

    The way the cascading filesystem works, is that it takes the namespace and the classname, and replaces all underscores and backslashes to forward slashes, and converts the result to lowercase.

    In your case, that leads to a file called ./classes/controller/controller/welcome.php.

    If you haven't changed your configuration, it will expect a class called "Controller_Controller_Welcome" in that file. Which isn't there.

    If you have (assume you have configured "Controller\" as prefix), it will check for "\Controller\Controller_Welcome". Which matches with what you have defined, and will be found.

    You can not use any other namespacing in App, the autoloader doesn't support it. It constructs classnames using underscored hardcoded.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion