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()) {
        extract($data);
        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.
LVL 1
umaximAsked:
Who is Participating?
 
Erdinç Güngör ÇorbacıConnect With a Mentor 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)) {
include("classfile.php");
}

you can deal with your auto loader or for a quick solution try to put your whole code inside
if (!class_exists("Widget", false)) { ....................  }
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.