Solved

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

Posted on 2008-10-29
18
632 Views
Last Modified: 2008-10-30
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
Comment
Question by:nanharbison
  • 8
  • 4
  • 4
  • +1
18 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 22837201
Somewhere, somehow, you are including the script a second time, without the _once :)
0
 
LVL 17

Author Comment

by:nanharbison
ID: 22837324
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
 
LVL 27

Expert Comment

by:yodercm
ID: 22837365
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:yodercm
ID: 22837369
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22837649
It's also possible the same function name is just used twice in different files, or even in the same file...
0
 
LVL 17

Author Comment

by:nanharbison
ID: 22839374
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
 
LVL 17

Author Comment

by:nanharbison
ID: 22839381
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
 
LVL 27

Accepted Solution

by:
yodercm earned 300 total points
ID: 22839804
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 22840354
You might also look out for "require_once" or "require" as well as the variations on "include"
0
 
LVL 17

Author Comment

by:nanharbison
ID: 22841333
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22841348
If you have textpad, you can use "find in files" and scan for "include" using REGEX.  It's a great program!
0
 
LVL 17

Author Comment

by:nanharbison
ID: 22841388
Ray, I use DreamWeaver, which has a global find, thanks though.
0
 
LVL 17

Author Comment

by:nanharbison
ID: 22845671
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
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 100 total points
ID: 22845695
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22845712
Are you interested in posting your code? I'd be happy to have a look through it...
0
 
LVL 17

Author Comment

by:nanharbison
ID: 22845833
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22845880
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
 
LVL 17

Author Comment

by:nanharbison
ID: 22846077
thanks Terry, I will try this.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question