Solved

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

Posted on 2008-10-29
18
607 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
Comment Utility
Somewhere, somehow, you are including the script a second time, without the _once :)
0
 
LVL 17

Author Comment

by:nanharbison
Comment Utility
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
Comment Utility
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
 
LVL 27

Expert Comment

by:yodercm
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
You might also look out for "require_once" or "require" as well as the variations on "include"
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 17

Author Comment

by:nanharbison
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Ray, I use DreamWeaver, which has a global find, thanks though.
0
 
LVL 17

Author Comment

by:nanharbison
Comment Utility
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
Comment Utility
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
Comment Utility
Are you interested in posting your code? I'd be happy to have a look through it...
0
 
LVL 17

Author Comment

by:nanharbison
Comment Utility
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
Comment Utility
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
Comment Utility
thanks Terry, I will try this.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now