_construct方法未被调用


_construct method is not called

我正在使用Netbeans编辑器。我在类中初始化了一个构造函数方法

class test {
  public $prop= 'i am class property';
  public function _construct(){
    echo 'hello class"', __CLASS__, '" is initiated';
  }
  public function setname($newvar){
    $this->prop=$newvar;
  }
  public function getname(){
    return $this->prop."</br>";
  }
}
$obj = new test ;
echo $obj->getname();

我的_construct方法不工作,不提供类名的输出

PHP中的构造函数以两个下划线开头,即__construct。当您使用一个下划线时,它只是一个方法,不会在创建对象时调用。

施工人员请参阅手册。

您应该使用两个下划线:__constructor如果您只使用一个,那么它只是一个方法。祝好运