tags:

views:

69

answers:

3

I've got the following code:

class the_class {
  private the_field;

  function the_class() {
    the_field = new other_class(); //has other_method()
  }

  function method() {
    $this->the_field->other_method(); //This doesn't seem to work
  }
}

Is there anything wrong with the syntax here? The method call always seems to return true, as if there is something wrong with the method call. Or is the method call all right and I should debug the other_method() itself?

TIA, Peter

+2  A: 

you need to use $this->the_field in the constructor too.

fabrik
+6  A: 

You need to use the dollar sign for variables. So your code would become thus:

class the_class {
  private $the_field;

  function the_class() {
    $this->the_field = new other_class();
    // assigning to $this->... assigns to class property
  }

  function method() {
    $this->the_field->other_method();
  }
}
Martin Bean
+3  A: 

You have missed PHP Variable notation and Class Property Access:

  • $ symbol is must for each PHP variable ( not CONSTANT ).
  • $this keyword/self variable is must to access current class or parent class member. In some cases you may/have to use 'self' or 'parent' keyword specially for static member.

    class the_class {
      private $the_field;//changes
    
    
      function the_class() {
        $this->the_field = new other_class(); //has other_method()//changes
      }
    
    
      function method() {
        $this->the_field->other_method(); //This doesn't seem to work
      }
    }
    
Sadat