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

PHP OOP: Is it possible to pass parameters to a class __destruct?

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

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
  }

}

,析构函数只有一个签名

void __destruct ( void )

Manual

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

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