Jump to content

Redeclaring a class issue!


onthespot

Recommended Posts

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 !

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.