Redeclare Problem

Posted on 2012-08-31
Last Modified: 2012-09-03
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.
Question by:umaxim
    LVL 9

    Accepted Solution

    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)) { ....................  }
    LVL 107

    Expert Comment

    by:Ray Paseur
    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now