Mitya
Mitya

Reputation: 34598

CodeIgniter: "Cannot redeclare class"

Admittedly my OOPHP is a little shaky but I can't see what's wrong with this. In one of my controllers I'm including a utils sheet which, like the controller, extends the base CI_Controller class. This throws the fatal error:

Fatal error: Cannot redeclare class Utils in {file path}\utils.php on line 88

Controller:

class Dashboard extends CI_Controller {

    public function __construct() {

        //call parent constructor
        parent::__construct();

        //load utils
        require 'application/helpers/utils.php'; //<-- utils loaded
        $this->utils = new Utils(); //<-- utils instantiated

        //load Dashboard model
        $this->utils->load->model('dashboard');

    }

    //etc...

}

utils.php:

class Utils extends CI_Controller {

    //prep for forms (on join or login views)
    public function prep_form() {
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
    }

    //etc - more util methods

}

Why does it think I'm RE-declaring Utils despite calling it and instantiating it only once? Weird thing is, I have another controller, for another part of the site, with this same pattern and it has no complaints.

Finally, I tried moving the require instruction outside the controller, and changing it to require_once just in case something really was calling it twice, and in both cases the page just hangs, eventually resolving with no source code sent to the browser.

Thanks in advance.

Upvotes: 2

Views: 12664

Answers (6)

Thomas FAUR&#201;
Thomas FAUR&#201;

Reputation: 67

try

require_once 'application/helpers/utils.php';

to avoid this error.

Upvotes: 1

Zeshan
Zeshan

Reputation: 2657

Rename

helpers/utils.php

to

helpers/Utils.php.

Servers other than windows treats class name should match with file name.

Hope it helps you.

Upvotes: 2

Egor Sazanovich
Egor Sazanovich

Reputation: 5089

I have no idea why You are trying to put class extended from CI_Controller to Your helper. Helpers for helper-functions (like sort MySQL result by key, etc). Maybe You need model?

If You need CodeIgniter's function at Your helper, just use get_instance() instead $this

function prep_form() {
    get_instance()->load->helper('form');
    get_instance()->load->library('form_validation');
    get_instance()->form_validation->set_error_delimiters('<li>', '</li>');
}

Upvotes: 1

deviloper
deviloper

Reputation: 7240

why dont you just call:

$this->load->library('utils'); // needs to be stored in the libraries directory

insead of

require 'application/helpers/utils.php'; //<-- utils loaded
$this->utils = new Utils(); //<-- utils instantiated

Upvotes: 1

KingCrunch
KingCrunch

Reputation: 132061

require 'application/helpers/utils.php'; //<-- utils loaded

This one is probably called twice. You either instanciate Dashboard twice, or you include that file somewhere else once more.

Upvotes: 0

George
George

Reputation: 3953

Use $this->load->helper () instead of require()

Upvotes: 1

Related Questions