PHP OOP:是否可以将参数传递给类__destruct?

时间:2016-12-30 03:58:11

标签: php oop destructor

如果需要,可以通过构造函数将参数传递给类。

class Test {

  public function __construct($echo) {
    echo $echo;
  }

}

$test = new Test('hello'); // Echos "hello"

有没有办法将参数传递给__destruct

class Test {

  public function __construct($echo) {
    echo $echo;
  }

  public function __destruct($string) { // Is this possible?
    // Do something with this string
  }

}

2 个答案:

答案 0 :(得分:1)

,析构函数只有一个签名

void __destruct ( void )

<强> Manual

答案 1 :(得分:1)

这是不可能的。 但您可以使用如下的实例字段:

class Test {
  var $value;
  public function __construct($echo) {
    this->value = $echo;
  }
  public function __destruct() {
    echo $this->value;
  }
}