Jump to content

"cannot redeclare" error (with diagram)


behrk2

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

untitled.bmp

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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 {

  // ...

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.