PHP中的OOP问题

Pet*_*ann -1 php oop syntax

我有以下代码:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

这里的语法有什么问题吗?方法调用似乎总是返回true,就好像方法调用有问题一样.或者方法调用是否正确,我应该调试other_method()本身?

TIA,彼得

Mar*_*ean 6

您需要使用美元符号表示变量.所以你的代码将成为:

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();
  }
}
Run Code Online (Sandbox Code Playgroud)