onthespot Posted June 29, 2011 Share Posted June 29, 2011 I have the following error Fatal error: Cannot redeclare class Database in /home/content/63/8026363/html/include/user_database.php on line 5 That links to the line class Database But I have not redeclared this anywhere I can see. I've checked all the include files and still can't see it. I have now changed the name to user_database1.php which is DEFINITELY only included once in my WHOLE system and I am still getting the same message! This only occurs in my root/admin directory. When I moved the file it occurs into the root directory and updated the include files from ../file.php to just file.php, it worked perfectly. I can't understand why having the file.php in the /admin directory and using ../ to include files isn't working! Can anyone offer any experience of this? Or a potential fix. I'll provide some code from the top of the file in question.. <?php include("../include/session.php"); include("../include/admin_database.php"); Clearly this is the problem but I can't understand why! Hope someone can help ! Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/ Share on other sites More sharing options...
TeNDoLLA Posted June 29, 2011 Share Posted June 29, 2011 Clearly you have included the class somewhere twice because it gives you that error. Try replacing include()'s with require_once()'s so it will not include the file anymore if it has been included once. Better way to do it to avoid problems like this on a larger projects. Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236197 Share on other sites More sharing options...
onthespot Posted June 29, 2011 Author Share Posted June 29, 2011 I can't understand it though. In the root, the files were fine. Once I go to /admin, there is a problem. Even though I am including all the files in the same way just with ../ in front! Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236198 Share on other sites More sharing options...
onthespot Posted June 29, 2011 Author Share Posted June 29, 2011 I have changed include to require_once throughout the project. Now it is complaining about session.php rather than user_database.php. Again, in the root directory, no problem. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236201 Share on other sites More sharing options...
TeNDoLLA Posted June 29, 2011 Share Posted June 29, 2011 So what is it complaining now? I can't read your mind, see any of your codes nor the error it is giving now. Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236203 Share on other sites More sharing options...
onthespot Posted June 29, 2011 Author Share Posted June 29, 2011 Fatal error: Cannot redeclare class Session in /home/content/63/8026363/html/include/session.php on line 7 Line 7 is where the class Session { starts. So the claim now is that I am redeclaring that class, when I clearly am not! Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236204 Share on other sites More sharing options...
TeNDoLLA Posted June 29, 2011 Share Posted June 29, 2011 You must have forgotten some include's or something. It clearly states you are re-declaring classs Session. Don't really know anything else that would give this error. Actually only other choice would be that there is a built-in class in PHP called 'Session' but I don't think that exists. If thats the case just change the name of the class in the classfile and see if it helps. Easy to test it out. Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236205 Share on other sites More sharing options...
onthespot Posted June 29, 2011 Author Share Posted June 29, 2011 Changed it to Session1 and same error but for Session1. What is really odd is that it seemed to work fine yesterday. Then i refreshed the page and this had happened. I made no changes, hmmmm. Could something have been changed by the host that could cause this? As I've said, it's really odd that the error only happens in the admin folder. If I move this same file into the root directory and change the links accordingly, it's fine. Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236209 Share on other sites More sharing options...
TeNDoLLA Posted June 29, 2011 Share Posted June 29, 2011 With the information provided, there is nothing more that comes up to my mind. Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236210 Share on other sites More sharing options...
onthespot Posted June 29, 2011 Author Share Posted June 29, 2011 The error is stating I am redeclaring a class, but if I remove the require_once for that class, it gives me an error because it needs to use functions within. I just can't understand how I am going to get around this. Never come across anything like it before. It just WILL NOT work if I am not in the root directory. The issue is clearly when I am in a folder onto of the root directory. 7 hours spent trying to fix this now and still no luck! Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236213 Share on other sites More sharing options...
JonnoTheDev Posted June 29, 2011 Share Posted June 29, 2011 If you are including a file twice via include() you will get that error. I wouldn't normally suggest this however, ammend your includes to include_once() or require_once(). It seems that you must have a poor application layout. A check you can perform when including class files: <?php if (!class_exists('FoobarClass')) { require_once('classes/foobar.class.php'); } $foobar = new FoobarClass(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240686-redeclaring-a-class-issue/#findComment-1236229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.