2

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • if you have need same method in two controllers, then you must write method in parent controller – Samar Haider Jun 18 '14 at 11:36
  • Why put the utility functions in a controller? Why not a helper or a library? – jleft Jun 18 '14 at 11:38
  • @Lefters - I wasn't clear; my utils are in a helper. It's just that they take the form of methods, i.e. they reference `$this`, so they're not procedural. Perhaps this makes helpers the wrong place for them. – Mitya Jun 18 '14 at 11:41
  • 2
    they say `Unlike most other systems in CodeIgniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.` – Vipin Kumar KM Jun 18 '14 at 11:42

6 Answers6

2

Rename

helpers/utils.php

to

helpers/Utils.php.

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

Hope it helps you.

Zeshan
  • 2,496
  • 3
  • 21
  • 26
1

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

George
  • 3,757
  • 9
  • 51
  • 86
1

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
M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76
  • Because utils (despite its possibly unsuitable name) has methods, i.e. which use `$this`, not just procedural functions. Perhaps I've gone about this in the wrong way. – Mitya Jun 18 '14 at 11:40
  • 1
    @Utkanos you can use get_instance() to get a ref of the CI object to use instead of $this in a helper. I usually $CI =& get_instance() and then use $CI as if it were $this – xd6_ Jun 18 '14 at 11:44
  • Ah, interesting re: `get_instance()`. How would your helpers access it - by having it passed to them as an argument? – Mitya Jun 18 '14 at 11:46
  • @Utkanos you would call that inside each helper function that needs it, no need to pass it as an arg. Also you can add the helper file to autoload.php so you can call these functions directly from anywhere without having to load anything or prefix it with $this-> or anything – xd6_ Jun 18 '14 at 11:59
1

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>');
}
Egor Sazanovich
  • 4,979
  • 5
  • 23
  • 37
  • The reason is this: my helpers contain methods, not procedural functions. That is, they reference `$this`. In other words, they contain methods that should be common - i.e. accessible to - all controllers. If there is a better way (and there may be), I'm all ears! – Mitya Jun 18 '14 at 11:42
  • 1
    @Utkanos so You should to extend Your base controller (http://ellislab.com/codeigniter/user-guide/general/core_classes.html) and extend Your controllers from it – Egor Sazanovich Jun 18 '14 at 11:44
  • OK, this is outside of my knowledge. I'll read the link you gave. Do you mean to say that, rather than my controllers directly extending `CI_Controller`, I introduce a custom base class, which sits between these two stages? – Mitya Jun 18 '14 at 11:46
  • @Utkanos Yep. Put `MY_Controller.php` to `app/core/`, which be like `class MY_Controller extends CI_Controller { function __construct() { parent::__construct();} function test(){echo "I'm here";}}` And at Your controller use it: `class Dashboard extends MY_Controller {function index(){$this->test();}}` – Egor Sazanovich Jun 18 '14 at 11:47
  • Thanks, I'll explore this. – Mitya Jun 18 '14 at 11:49
  • @Utkanos try the answer first its perfect , `Parent controller` will work too. just an edit `$CI= &get_instance();` and you can use `$CI->` similar to `$this->` – Karan Thakkar Jun 18 '14 at 11:51
  • @utkanos, y load model via util – George Jun 18 '14 at 11:57
1

try

require_once 'application/helpers/utils.php';

to avoid this error.

Thomas FAURÉ
  • 67
  • 1
  • 1
  • 8
0
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.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • That's what I thought but there's no trace of this. In fact, even though the guy deleted his answer, changing the model name from 'modelname' to 'modelname_model' fixed this for some reason. Confused. Thanks anyway. – Mitya Jun 18 '14 at 11:43