Support » Fixing WordPress » Cannot redeclare function

  • burbidgewebconsulting

    (@burbidgewebconsulting)


    It has been a long while since I’ve used PHP so hopefully there is just a syntax error I am missing. When I try to insatiate a new CSVFile in class_add_product, I get the error: Cannot redeclare QuadLayers_init() (previously declared in …php:22) in … on line 22. Like it’s trying to go back to add_product and run the QuadLayers_init() again. How do I create a new object of another class type within a class. Or if I can’t, what’s the proper way to do this?

    In add_product.php:

    
    if(!defined('ABSPATH')){die('-1');}
    function QuadLayers_init()
    	{
    		require_once plugin_dir_path( __FILE__ ).'class_add_product.php';
    		require_once plugin_dir_path( __FILE__ ).'class_csvfile.php';
    		$run = new QuadLayers_class;
    	}
    
            QuadLayers_init();
    
    In class_add_product.php:
    class QuadLayers_class{
          public $csv_filename = 'sample_data.csv';
    
          public function __construct(){
    		add_action('activated_plugin',array($this,'upload_products'));
    		}
    		    $csv_file = new CSVFile('sample_data.csv');
    		}
    
    		public function import_csv(){
    		    $csv = new CSVFile($this->csv_filename);
    		    return $csv;
                    }
         }
    }
    
    In class_csvfile.php:
    class CSVFile extends SplFileObject
    {
        private $keys;
    
        public function __construct($file)
        {
            parent::__construct($file);
            $this->setFlags(SplFileObject::READ_CSV);
        }
    
        public function rewind()
        {
            parent::rewind();
            $this->keys = parent::current();
            parent::next();
        }
    
        public function current()
        {
            return array_combine($this->keys, parent::current());
        }
    
        public function getKeys()
        {
            return $this->keys;
        }
    }
    
    • This topic was modified 5 days, 14 hours ago by burbidgewebconsulting.
    • This topic was modified 5 days, 14 hours ago by burbidgewebconsulting.
    • This topic was modified 5 days, 3 hours ago by Yui. Reason: please use CODE button for code formatting
  • You must be logged in to reply to this topic.