ciscocert
ciscocert

Reputation: 255

APC, PHP and two classes which require each other

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?

Upvotes: 0

Views: 645

Answers (2)

SeanDowney
SeanDowney

Reputation: 17744

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

Upvotes: 0

tereško
tereško

Reputation: 58444

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

Upvotes: 1

Related Questions