PHP Cannot redeclare function error: Why am I getting this error?

102 Views Asked by At

I have two PHP scripts that include the same include file. Also, for testing purposes, I declared the same function that performs SOAP operations in both scripts. In other words, my scripts look like this:

#script1.php
include('include.php');

someFn();
function someFn() {
  //SOAP code here
}

#script2.php
include('include.php');

someFn();
function someFn() {
  //SOAP code here
}

Whenever I try to run one script and then the other, I get the following error:
PHP Fatal error: Cannot redeclare someFn() (previously declared in script1.php) in script2.php

I cannot understand why I'm getting this error. I've coded similar situations before, but never gotten this error, so I suspect that the executing of the SOAP code (specifically, the new SoapClient constructor and $client->__setSoapHeaders method) is causing this error.

What's also interesting is if I comment out the include file (which, by the way, does not have someFn defined in it) and then rerun the script, I don't get any errors and everything is fine.

What is happening here?
Thank you.

Edit: After playing around some more, I found out that if I have the following situation:

#script1.php
include('include.php'); //someFn defined in include.php

someFn();

#script2.php
someFn();
function someFn() {
  //SOAP code here
}

And then run script2.php and then script1.php after that, I get the same error. It's almost as if after I run script2.php and close it, the process continues in PHP so that when I run script1.php, I get the error.

Any help would be greatly appreciated.
Thank you.

Edit #2: This is honestly the most confusing thing ever. I started over by creating two brand new files in the same directory as the other files and then all I added to both files was the following code:

<?php

  include('include.php');

And when I run either file, I get all the SOAP output that was generated from the other files in the directory. I haven't bothered trying to restart the server, but at this point, I can only assume that something really weird is cached on the server.

Anyway, I resolved the issue by simply creating a new directory and then moving the file I wanted to that directory and re-executing it. By doing that, I got no errors and everything is fine.

Thanks everyone for your assistance.

0

There are 0 best solutions below