【问题标题】:PHP OOP: Is it possible to pass parameters to a class __destruct?PHP OOP:是否可以将参数传递给类__destruct?
【发布时间】:2017-05-14 09:52:09
【问题描述】:

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

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
  }

}

【问题讨论】:

    标签: php oop destructor


    【解决方案1】:

    没有,析构函数只有一个签名

    void __destruct ( void )
    

    Manual

    【讨论】:

    • 谢谢,我似乎在任何地方都找不到。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 2011-04-22
      • 2020-09-14
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      相关资源
      最近更新 更多