2

In my PHP app, I have several calls to require_once. On my development PC this works fine and doesn't try to include the same file multiple times. However when I moved it to my production server I'm getting an error

cannot redeclare class myClass

After searching through the code I've found that this happens just after a call to require_once, so it must be that which is causing it.

I've searched through the entire project and this class is definitely only declared in one file, and it's only ever included through require_once. Is there some weird PHP config that would make require_once behave differently on the production server?

Thanks

doubleDown
  • 8,048
  • 1
  • 32
  • 48
user1578653
  • 4,888
  • 16
  • 46
  • 74

2 Answers2

0

Put the following at the top of your included class file:

if( class_exists( "<yourclassname>"))
{
throw new Exception("Class already defined");
}

You will now receive a clear error message where the class is re-called.

Luceos
  • 6,629
  • 1
  • 35
  • 65
0

OK, this obnoxious problem can be caused by one of two things of which I'm aware:

symlinks; if loading from a file and a symlink pointing to the file

on OS X (and maybe Windows), the filenames are case-insensitive; PHP require_once is not

This had me confused for several hours today. I usually work in Linux, but I was forced onto a Mac where this becomes a problem.

rich remer
  • 3,407
  • 2
  • 34
  • 47