Find where a class was instantiated

2019-05-29 23:03发布

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.

3条回答
唯我独甜
2楼-- · 2019-05-29 23:48

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();
查看更多
对你真心纯属浪费
3楼-- · 2019-05-29 23:54

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

查看更多
看我几分像从前
4楼-- · 2019-05-30 00:04

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.

查看更多
登录 后发表回答