?
Solved

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

Posted on 2008-10-29
18
Medium Priority
?
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 4
  • +1
18 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
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:Cornelia Yoder
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
Technology Partners: 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!

 
LVL 27

Expert Comment

by:Cornelia Yoder
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:
Cornelia Yoder earned 1200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 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 111

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

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!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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