Roch
Roch

Reputation: 22061

Find where a class was instantiated

I have trying to solve the error : Fatal error: Cannot redeclare class

I have been looking everywhere and I can't find where the class was instantiated.

Is there anyway I can print debug info about the existing instance of that class.

Upvotes: 1

Views: 586

Answers (3)

Jens A. Koch
Jens A. Koch

Reputation: 41796

You can find out, where an object was instantiated by using var_dump(debug_backtrace()); and looking at the call stack.

Upvotes: 0

user187291
user187291

Reputation: 53960

Yes, stupid php doesn't tell you where the class was declared. Try the following (immediately before fatal error line)

$r = new ReflectionClass("YourClassName"); echo $r->getStartLine();

Upvotes: 3

cletus
cletus

Reputation: 625447

Chances are you are importing the file that declares the class more than once. This can be symptomatic of includes/requires getting out of control so you may need to simply your structure.

One alternative approach is to use autoload to load classes to avoid this kind of problem. Another is to only use include_once or require_once. I generally prefer to use require with a simple structure.

Upvotes: 3

Related Questions