SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot redeclare class

    So i have been working on a small framework for myself and i ran into a problem i really can't seem to figure out how or why i have this problem. i get an error when i try to use any class that is loaded through my autoload method "Cannot redeclare class" the class name bla bla so the error says it's on line 5 of my config class where i actually create the class name. So im really stuck and have no clue in witch way to go.

    the classes below are the classes i use to call and load the config class and the config class it' self.

    obj_bootstrap.php
    PHP Code:
    define('FW_DIR'  'framework');
    define('APP_DIR' 'application');


    define('DS'  '/');
    define('EXT' '.php');


    define('VER' '0.0.1');
    define('PHP' '5.3.2');


    define('CWD' str_replace('\\' '/' getcwd()) . DS);
    define('FW'  CWD FW_DIR DS);
    define('APP' CWD APP_DIR DS);


    if(
    PHP >= PHP_VERSION){
        exit(
    'your php version is not compatible with our supported versions of php.  
             Please upgrade your version of php at minimum to version ' 
    PHP);
    }

    require 
    FW 'obj_loader' EXT;
    spl_autoload_register(array('obj_loader' 'autoloader'));


    obj_config::getItem('db_server'); 


    obj_loader.php
    PHP Code:
    class obj_loader{

        private static 
    $_loaded = array();
        
        private static 
    $_root;
        
        public static function 
    setRootDir($root_dir ''){
            if(
    $root_dir !== '' && is_string($root_dir)){
                return 
    self::$_root = (string)$root_dir;
            }elseif(
    $root_dir !== '' && is_array($root_dir)){
                return 
    self::$_root $root_dir;
            }else{
                return 
    false;
            }
        }
        
        public static function 
    loadFile($filename $dir ''){
            if(isset(
    self::$_loaded[$filename])){
                require 
    self::$_loaded[$filename];
            }
            
            
    $_roots = array(FW APP self::$_root);
            
            foreach(
    $_roots as $base){
                if(
    $dir === '' && file_exists($base $filename EXT)){
                    require 
    $base $filename EXT;
                    
    self::$_loaded[$filename] = $base $filename EXT;
                }elseif(
    $dir !== '' && file_exists($base $dir DS $filename EXT)){
                    require 
    $base $dir DS $filename EXT;
                    
    self::$_loaded[$filename] = $base $dir DS $filename EXT;
                }else{
                    return 
    false;
                }
            }
        }
        
        public static function 
    loadClass($classname $dir ''){
            if(
    self::loadFile($classname $dir) !== false){
                
    self::loadFile($classname $dir);
                if(!
    class_exists($classname false)){
                    throw new 
    obj_exception('Load Error: seems the class '.$classname.' was not loaded properly');
                }
            }else{
                return 
    false;
            }
        }
        
        public static function 
    loadLibrary($classname){
            
    self::loadClass($classname);
        }
        
        public static function 
    loadController($classname){
            
    self::loadClass($classname 'controllers');
        }
        
        public static function 
    loadModel($classname){
             
    self::loadClass($classname 'models');
        }
        
        public static function 
    loadView($classname){
            
    self::loadClass($classname 'views');
        }
        
        public static function 
    loadExtension($classname){
             
    self::loadClass($classname 'extension');
        }

        public static function 
    loadModuleController($controller $module){
             
    self::loadClass($controller 'modules' DS $module);
        }
        
        public static function 
    loadModuleModel($model $module){
             
    self::loadClass($model 'modules' DS $module);
        }
        
        public static function 
    loadModuleView($view $module){
             
    self::loadClass($view 'modules' DS $module);
        }
        
        public static function 
    autoloader($classname){
            if(
    self::loadFile($classname) !== false){
                
    self::loadFile($classname);
            }elseif(
    self::loadFile($classname 'controllers') !== false){
                
    self::loadFile($classname 'controllers');
            }elseif(
    self::loadFile($classname 'models') !== false){
                
    self::loadFile($classname 'models');
            }elseif(
    self::loadFile($classname 'views') !== false){
                
    self::loadFile($classname 'views');
            }elseif(
    self::loadFile($classname 'extensions') !== false){
                
    self::loadFile($classname 'extensions');
            }else{
                require 
    FW 'obj_exception' EXT;
                throw new 
    obj_exception('Autoload Error: the classname '
                                        
    .(string)$classname.' could not be located or loaded by the autoloader');
            }
        }
        
        public static function 
    registerAutoloader(){
            
    spl_autoload_register(array('obj_loader' 'autoloader'));
        }
        
        

    obj_config class
    PHP Code:
    bj_loader::loadFile('obj_settings' 'settings');

    class 
    obj_config{
        
        private 
    $_config = array();
        
        private 
    $_config_count 0;
        
        private static 
    $_instance;
        
        private function 
    __construct(){}
        
        protected static function 
    instance(){
            if(!(
    self::$_instance instanceof self)){
                
    self::$_instance = new self;
            }
            
            
            return 
    self::$_instance;
        }
        
        protected function 
    set($key $data){
            if(!isset(
    $this->_config[$key])){
                
    $this->_config[$key] = $data;
                
    $this->_config_count++;
                return 
    true;
            }
            return 
    false;
        }
        
        protected function 
    get($key){
            if(isset(
    $this->_config[$key])){
                return 
    $this->_config[$key];
            }
            
            return 
    false;
        }
        
        protected function 
    clean($key){
            if(isset(
    $this->_config[$key])){
                unset(
    $this->_config[$key]);
                return 
    true;
            }
            
            return 
    false;
        }
        
        protected function 
    getAll(){
            return 
    $this->_config;
        }
        
        protected function 
    getCount(){
            
    $count $this->_config_count;
            return 
    $count;
        }
        
        public static function 
    setItem($key $data){
            return 
    self::instance()->set($key $data);
        }
        
        public static function 
    getItem($key){
            return 
    self::instance()->get($key);
        }
        
        public static function 
    deleteItem($key){
            return 
    self::instance()->clean($key);
        }
        
        public static function 
    getConfig(){
            return 
    self::instance()->getAll();
        }
        
        public static function 
    countConfigItems(){
            return 
    self::instance()->getCount();
        }


  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I'm having trouble following your code but, in this case, the error probably means you are trying to load the same file twice. Putting an echo statement in your loadFile code might help.

    This doesn't seem to make much sense:
    if(self::loadFile($classname) !== false){
    self::loadFile($classname);
    You call loadFile once and it it doesn't return false call it a second time?

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i noticed the same issue as you did so i fixed it to just return on a success and i still get the same error.

  4. #4
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,026
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quick question - but why run a version level check every time the script runs? Put that code in the installer and be done with it - it has no business in the main script.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •