PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a problem with a class (X) I want to execute one class (Z) function foo() bust I get an "Fatal error: Using $this when not in object contex" when I call it, any idea what's wrong or if this is possible.
class Z {
function foo() {
// do somethig
}
}
class X {
$x
function __contruct() {
$this->x = new z();
}
function var() {
$this->x->foo(); // Fatal error: Using $this when not in object contex
}
}
// Class declaration here
class Y {
function __contruct() {
$z = new z();
$x = new x();
}
}
Last edited by Benjamin on Thu Jul 29, 2010 7:22 pm, edited 1 time in total.
Reason:Added [syntax=php] tags.
You need to make foo() a static method. You can then call it with classname::foo(). You can't use $this unless an object has been instantiated. Use self:: in your static method instead.