0

I'm managing an PHP application and we want to enable APC now. The problem is that we have two classes which require_once each other. A very basic example would look like:

in class_a.php

require_once('path/to/class_b.php)';

class a extends something {
    //
}

in class_b.php

require_once('path/to/class_a.php');

class b extends something2 {
    //    
}

However, when we enable APC, there is an "[apc-error] Cannot redeclare class class_b in class_b.php". Ok, that's because the class has been already loaded via the require_once() in class_a.php so if some 3th file requre class_b.php, APC will raise the error.

How to solve this "circular reference-like" issue?

ciscocert
  • 255
  • 4
  • 10
  • Which version of PHP do you have? Make sure the paths are the exact SAME. Make sure you do not have a lingering file with the same name somewhere in your include_path if you do not specify full path. – roychri Mar 13 '12 at 17:23
  • try to use autoloaders - this will prevent you to fallback in such kind of issues – Mikhus Mar 13 '12 at 17:32
  • PHP 5.3 and APC is the very latest version. All include path are absolute and exactly the same. – ciscocert Mar 13 '12 at 18:35

2 Answers2

1

The best way to solve this would be to get rid of the circular dependency itself. I thing it actually qualifies as Code Smell.

Try following instructions in this article. It should provide you with an alternative approach. I just hope that you can _read_ Java ...

tereško
  • 58,060
  • 25
  • 98
  • 150
0

Apparently there is a "feature" that will allow you to override the require_once calls and allow them to be included multiple times. Since it appears you are using require_once, this appears to be your issue. To disable this check for the setting apc.include_once_override in php.ini or add in

[apc]
apc.include_once_override = 0

This setting has known issues with duplicate / not found classes etc. See if this helps

SeanDowney
  • 17,368
  • 20
  • 81
  • 90