Forum Moderators: coopster
I'm writing a class with some debugging functions, but it doesn't work... This is a simplified version, but the error is still showing up:
Fatal error: Using $this when not in object context in * on line 20
<?class A {
var $debug = array();
function setDebug ($var) {
$this->debug[microtime()] = $var;
}
function getDebug () {
return $this->debug;
}
}
class B extends A {
function fooBar () { // some function ...
$this->setDebug("Some debug message"); // ... adds a debug message
}
}
$foo = new A;
B::fooBar();
print_r($foo->getDebug);
?>
Does somebody know what I'm doing wrong? I would like to add debug messages in class B with the function defined in class A.
Cheers,
Thijs
Remember, $this refers to the current instance of the class which is an object, not a class.
When you do this:
$foo = new A;
B::fooBar();
There is no instance, no object, when you use the class resolution operator :: to access the method. So $this doesn't refer to anything. It is undefined. Therefore you are "using $this when not in object context".
If you do
$foo = new B;
$foo->fooBar();
That should work.