• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

PHP5 include_once is not working when a file is actually included more than once

I thought if I used include_once, even if a file is included more than once, it will only actually be included once. However, I have a file with all the database functions for the website in one file, and sometimes I have included it twice, and I get this error:

Fatal error: Cannot redeclare (function name here, the first function in the file).
So if you include a file that has functions in it, then include_once won't work, or what is going on here?

I am pretty sure I didn't get this error in PHP4, but I am getting the site ready to upgrade to PHP 5, and now I get these errors.

Thanks!
0
nanharbison
Asked:
nanharbison
  • 8
  • 4
  • 4
  • +1
3 Solutions
 
Cornelia YoderArtistCommented:
Somewhere, somehow, you are including the script a second time, without the _once :)
0
 
nanharbisonAuthor Commented:
I did a global find in ALL the files on the website, in fact I found every time this file was included in any form, and none are included or required without the _once.
0
 
Cornelia YoderArtistCommented:
I use include_once for function scripts all the time, so it works.  Is there any chance that the function in question is in another different script?  or twice in this one?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Cornelia YoderArtistCommented:
By the way, the reason you didn't get the error in php4 is because php4 allowed redefinition of a function.  php5 doesn't.
0
 
Terry WoodsIT GuruCommented:
It's also possible the same function name is just used twice in different files, or even in the same file...
0
 
nanharbisonAuthor Commented:
The functions for the database are all in one script, and nowhere else, so there is no chance that the same function is being in different files. In fact, the function name that I get the error message for is in my DatabaseFunctions.php file, but is not being called on the page that gives me the error. I can't figure this out!
0
 
nanharbisonAuthor Commented:
And this funtion that is in the error - I tried removing it from the file, and I got the same error message for the next function in the file.
0
 
Cornelia YoderArtistCommented:
The error occurs when the function is redeclared, it doesn't need to be called, so that part is normal.

Create a new script, as follows:

<?php
echo "Debug 1<br>";
include_once "DatabaseFunctions.php";
echo "Debug 2<br>";
include_once "DatabaseFunctions.php";
echo "Debug 3<br>";
include_once "DatabaseFunctions.php";
echo "Debug 4<br>";
exit();
?>

and run it.  What happens?
0
 
Ray PaseurCommented:
You might also look out for "require_once" or "require" as well as the variations on "include"
0
 
nanharbisonAuthor Commented:
Ray - I did check for all require and require_once.
yodercm - this script worked just fine, no error messages, so I must have an include or require in here somewhere without the _once, huh?
I guess I will have to check file by file, ugh.
0
 
Ray PaseurCommented:
If you have textpad, you can use "find in files" and scan for "include" using REGEX.  It's a great program!
0
 
nanharbisonAuthor Commented:
Ray, I use DreamWeaver, which has a global find, thanks though.
0
 
nanharbisonAuthor Commented:
I have looked over and over again... does it matter if sometimes I use require_once and sometimes include_once? I can't find a definitive answer to what the difference is.
0
 
Terry WoodsIT GuruCommented:
No, it doesn't. The difference is that require produces a fatal error if the file can not be accessed, whereas include will just give a warning.
0
 
Terry WoodsIT GuruCommented:
Are you interested in posting your code? I'd be happy to have a look through it...
0
 
nanharbisonAuthor Commented:
This problem spreads out over dozens of files. I use templates, and then include everything I need on the page, the navigation include, the databasefunctions file, etc I think I have to rethink the whole set up.....
thanks to everyone who tried to help.
0
 
Terry WoodsIT GuruCommented:
It may be worth expanding on the testing script that yodercm asked you to run.

Firstly, you'd need to figure out a hierarchy of how each file relates to other files. eg
DatabaseFunctions.php is required/included by a.php, b.php etc
a.php is required/included by c.php...

Then you work your way up the tree in the order you try testing your scripts:
<?php
echo "Debug 1<br>";
include_once "DatabaseFunctions.php";
echo "Debug 2<br>";
include_once "a.php";
?>

then try:

<?php
echo "Debug 1<br>";
include_once "DatabaseFunctions.php";
echo "Debug 2<br>";
include_once "b.php";
?>

then try:

<?php
echo "Debug 1<br>";
include_once "DatabaseFunctions.php";
echo "Debug 2<br>";
include_once "a.php";
echo "Debug 3<br>";
include_once "c.php";
?>

That may help you pinpoint the problem. Best of luck, and thanks for the points!
0
 
nanharbisonAuthor Commented:
thanks Terry, I will try this.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now