[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Redeclare Problem

Hi i have this kind of script for CI
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Widget
    public $module_path;
    function run($file) {        
        $args = func_get_args();

        $module = '';
        /* is module in filename? */
        if (($pos = strrpos($file, '/')) !== FALSE) {
            $module = substr($file, 0, $pos);
            $file = substr($file, $pos + 1);

            include './application/widgets/'.$file.".php";
            $file = ucfirst($file);
            $widget = new $file();
            global $widget;
            $widget->module_path = $path;    
        return call_user_func_array(array($widget, 'run'), array_slice($args, 1));    
    function render($view, $data = array()) {
        include './application/widgets/views/'.$view.EXT;

    function load($object) {
        $this->$object = load_class(ucfirst($object));

    function __get($var) {
        global $CI;
        return $CI->$var;

Open in new window

SO it load class and then run it. The problem when i run script once it fine. But when i run script second time with the same class name it give me error Fatal error: Cannot redeclare class Banners in /httpdocs/application/widgets/banners.php on line 20

So question is it any way to make class end and then run again.
1 Solution
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
It seems theres a problem with your  autoloader . Is it CI s own code ?
because autoloader wouldn't try to load a loaded class . It should contain a check statement for that. like this

if (!class_exists($class, false)) {

you can deal with your auto loader or for a quick solution try to put your whole code inside
if (!class_exists("Widget", false)) { ....................  }
Ray PaseurCommented:
I think erdincgc is on the right path.  Another possibility is to consider using include_once() when you only want one copy of a file included, such as when the file contains a function or class definition.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now