Page 1 of 1

Class problem

Posted: Thu Jul 29, 2010 7:02 pm
by bolven
Hello all,

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.

I'll appreciate your help.

Thank you

Code: Select all

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();
    }
}

Re: Class problem

Posted: Fri Jul 30, 2010 2:03 am
by cpetercarter
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.

Re: Class problem

Posted: Fri Jul 30, 2010 2:45 am
by anilnakkani
Hi, This is Anil,
Yes we can use Scope resolution operator can use ouside of class functions can call...

Example: ClassName::FunctionName();

Thanks
Anil.N
Thought Radius - Experts for Partners

Re: Class problem

Posted: Fri Jul 30, 2010 3:40 am
by Christopher
You need to create an object from the class:

Code: Select all

$z = new Z();
$z->foo();