Image

Imagehipmaestro wrote in Imagephp

debug_backtrace() bug

I've found what I think is a bug in PHP 5.0.1, and I thought perhaps someone who is running 5.0.2 might try this code and see if the same problem exists there, before I submit a bug report. (Or, tell me I'm stupid and that it's not actually a bug... either way.)


The problem is that debug_backtrace() incorrectly reports the class when it involves a method that's been overridden in a subclass. For example, this code:


class A
{
  function __construct()
  {
    $bt = debug_backtrace();
    print "<p>constructor for class A:<br>";
    print "class according to __CLASS__: ".__CLASS__."<br>";
    print "class according to debug_backtrace: ".$bt[0]["class"]."</p>";
  }
}

class B extends A
{
  function __construct()
  {
    $bt = debug_backtrace();
    print "<p>constructor for class B:<br>";
    print "class according to __CLASS__: ".__CLASS__."<br>";
    print "class according to debug_backtrace: ".$bt[0]["class"]."</p>";

    parent::__construct();
  }
}

print "<p>Instantiating a B object:</p>";
$b = new B();

print "<p>---</p>";

print "<p>Instantiating an A object:</p>";
$a = new A();


produces this output:


Instantiating a B object:

constructor for class B:
class according to __CLASS__: B
class according to debug_backtrace: B

constructor for class A:
class according to __CLASS__: A
class according to debug_backtrace: B

---

Instantiating an A object:

constructor for class A:
class according to __CLASS__: A
class according to debug_backtrace: A


Notice that in the constructor for class A, when invoked on a B object, the backtrace reports that the current class is B instead of the actual class, A. The magic constant __CLASS__ gets it right.


Any help is appreciated. Thanks.