2

I have 3 class as A, B and C like the following:

a.php

<?php
abstract class A {
    public static function getInstance() {
        $child = get_called_class();
        $instance = new $child;
    }
}
?>

b.php

<?php
class B extends A {
    const MY_CONSTANT = SomeClass::UNKNOWN_CONSTANT;
}
?>

c.php

<?php

include 'a.php';
include 'b.php';
B::getInstance();
//Here will got an error 
//Fatal error: Class 'SomeClass' not found in a.php on line 5 

?>

Clearly there is an error in the B class, but when I call B::getInstance() method, that error message is "Fatal error: Class 'SomeClass' not found in a.php on line 5"

I don't understand why that error message does not tell me that error in "b.php on line 3"? How to know that real error place and debug it?

EDIT 1

I think PHP should have told me error in b.php on line 3, otherwise I will hard to debug.

4
  • php.net/manual/en/function.debug-backtrace.php
    – goat
    Commented Jan 16, 2013 at 20:26
  • 1
    I'm using Xdebug for that purpose. It shows the stacktrace and a more annoying error message that plain PHP. In my opinion, more annoying is good on the development machine.
    – Raul Pinto
    Commented Jan 16, 2013 at 20:28
  • @joe I want to suggest you to use an arbitrary IDE, that will directly show you such mistakes :)
    – KingCrunch
    Commented Jan 16, 2013 at 21:39
  • If you're looking to rant about PHP, you can join the legions of programmers out there... Just not here. Here is for solving problems. We can't really solve rants. If you can improve your question and reduce the rant, this question stands a good chance of being re-opened. Commented Jan 17, 2013 at 2:23

2 Answers 2

2

The line number will always be the line that triggered the exception or error.

Take a look at the PHP function debug_backtrace(). It provides information on the call stack which is what you're looking for.

1
  • Or even better: A debugger :D
    – KingCrunch
    Commented Jan 16, 2013 at 21:35
0

The non-static MY_CONSTANT isn't created until you try to create an instance of B. B's static method is the static method inherited from A, which then tries to instantiate B and finds that one of its non-static values explodes.

1
  • But I think PHP should have told me error in b.php on line 3, otherwise I will hard to debug.
    – Jasper
    Commented Jan 16, 2013 at 20:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.