behrk2 Posted January 24, 2009 Share Posted January 24, 2009 Hey guys, I posted something really confusing before, but have since refactored my code to something much more simple. Anyways, I have the following scripts: run_auto.php auto_test.php I am getting the following error: "Fatal error: Cannot redeclare sdb_setup()", and I'm not sure why. Here are some code segments: run_auto.php: require_once('auto_test.php'); $a_t = new auto_test(); . . . . } else if ($dir == '-regression' && $arg == '') { $count = 1; $dir_array = $a_t -> collect_dirs(); foreach ($dir_array AS $dir) { echo("CURRENTLY RUNNING $dir, $count out of ".count($dir_array)." test cases.\n\n\n"); $a_t -> prep($DB, $ode, $sel, $schema, $dir); $a_t -> sdb_script($DB, $ode, $sel, $schema, $dir); $a_t -> pdb_script($DB, $ode, $sel, $schema, $dir); $a_t -> test_script($DB, $ode, $sel, $schema, $dir); $err = $a_t -> regress_test($DB, $ode, $sel, $schema, $dir); $a_t -> write_dirs($DB, $ode, $sel, $schema, $dir, $err); $clear = exec("clear"); echo "$clear"; $count++; } auto_test.php: public function sdb_script($DB, $ode, $sel, $schema, $dir) { require_once "$dir/sdb_script.php"; try { sdb_setup($DB, $ode, $sel, $schema); } catch (Exception $e) { $file = fopen($dir.'/'.$dir.'-sdb.error', 'w'); fwrite($file, $e->getTraceAsString()); fwrite($file, $e->getMessage()); fclose($file); echo "\n"; echo "...ERROR: See $dir/$dir-sdb.error"; echo "\n"; exit(1); } So, run_auto.php (which requires_once auto_test.php), instantiates an object of auto_test.php, and calls upon multiple functions, one being sdb_script(). The sdb_script() calls the function sdb_setup from another file, which I have require_once INSIDE the sdb_script() function...it has to be that way, I cannot put it outside of that function. Does anyone know how I can fix this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/ Share on other sites More sharing options...
rubing Posted January 24, 2009 Share Posted January 24, 2009 It sounds like your classes are designed in a bad way. Why don't you set up a parent class with all of the main methods (functions) that you want and then child classes, which extend from it. you probably don't want to include a new file that's gonna overwrite some existing method. Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-744910 Share on other sites More sharing options...
behrk2 Posted January 24, 2009 Author Share Posted January 24, 2009 Hm, I tried making a child class of which is called by the parent, but still the same error... Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-744942 Share on other sites More sharing options...
rubing Posted January 24, 2009 Share Posted January 24, 2009 Post your entire script and the exact error message Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-744954 Share on other sites More sharing options...
behrk2 Posted January 24, 2009 Author Share Posted January 24, 2009 Hey guys, so I'm STILL receiving this error, and it's driving me NUTS! It's been really hard to explain, so no one seems to understand it, but I have attached a diagram this time. Here is the error and the diagram. Can anyone see what's going on? Thanks... Fatal error: Cannot redeclare sdb_setup() (previously declared in /home/kevin/projects/hydro4ge/src/sel/tc_add_column/sdb_script.php:9) in /home/kevin/projects/hydro4ge/src/sel/tc_add_column_default/sdb_script.php on line 24 Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745472 Share on other sites More sharing options...
DeanWhitehouse Posted January 24, 2009 Share Posted January 24, 2009 It means you are trying to re-declare a function you have already declared (i think, pretty sure). Are you including a page with this function declared or included on it? Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745477 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 what's in sdb_setup()? Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745478 Share on other sites More sharing options...
behrk2 Posted January 24, 2009 Author Share Posted January 24, 2009 sdb_setup() is a function that just contains a bunch of postgresql queries. In auto_test.php, there is basically a loop that runs through a collection of $dirs (stored in an array), and for each $dir, sdb_setup() is called. But the sdb_setup called is from a different file each time, even though its the same function name... Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745484 Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 the problem is you cannot do this: function sdb_setup($args){ // do stuff } function sdb_setup($args){ // do different stuff } you must name the function something else. eg: function sdb_setup($args){ // do stuff } function sdb_setup2($args){ // do different stuff } Also, do not include any files that defines functions or classes more than once in a single execution. Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745516 Share on other sites More sharing options...
behrk2 Posted January 24, 2009 Author Share Posted January 24, 2009 Ah, ok. I thought it would be okay to do since sdb_setup is called from a different file every time. Do you have any suggestions as to how I can restructure my classes to achieve what I'm trying to do? I would greatly appreciate it... Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745529 Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 I dont think you have explained what your trying to do (what is the 1st time, 2nd time, 3rd time?) EDIT: damn Y key - lol Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745533 Share on other sites More sharing options...
behrk2 Posted January 24, 2009 Author Share Posted January 24, 2009 Basically, run_auto.php calls a function in auto_test.php, which collects a bunch of directory names into an array. Then, in run_auto.php, I have a for loop that looks like this: foreach ($dir_array AS $dir) { echo("CURRENTLY RUNNING $dir, $count out of ".count($di$ $a_t -> prep($DB, $ode, $sel, $schema, $dir); $a_t -> sdb_script($DB, $ode, $sel, $schema, $d$ $a_t -> pdb_script($DB, $ode, $sel, $schema, $d$ $a_t -> test_script($DB, $ode, $sel, $schema, $$ $err = $a_t -> regress_test($DB, $ode, $sel, $s$ $a_t -> write_dirs($DB, $ode, $sel, $schema, $d$ $clear = exec("clear"); echo "$clear"; $count++; } $a_t is an object of auto_test.php. So, for every $dir, these functions from auto_test.php will run. The one function, sdb_script, will call a function, sdb_setup, which is from a file in each different directory ($dir). And therein lies the error... Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745544 Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 wait, so in the for loop, you are including different files with the same function name? if so you cannot do this, either name the functions differently in each file or look into classes where you can extend another class. class Extension extends auto_test { // ... } Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745552 Share on other sites More sharing options...
behrk2 Posted January 24, 2009 Author Share Posted January 24, 2009 Ok, thanks. I suppose I can name each function differently, and have each name match its $dir name. Or, what if, within the foreach statement, I call that sdb_setup() function directly (from sdb_script.php), instead of doing it within auto_test.php? I guess I'm trying to do this as efficiently as possible. Quote Link to comment https://forums.phpfreaks.com/topic/142198-cannot-redeclare-error-with-diagram/#findComment-745554 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.