如果你调用从析构函数构造会发生什么?(What happens if you call a cons

2019-07-29 05:12发布

调用__construct()从__destruct()的函数,

<?php

public function __construct() {
    echo "Hi";
}

public function __destruct() {
    $this->__construct();
}

?>

它会创建无限循环?

Answer 1:

没有,但这会:

class Test {

    public function __construct() {
        echo "Hi";
    }

    public function __destruct() {
         new Test();
    }

}

new Test();

例如: http://ideone.com/94XUg



Answer 2:

不,不会。 __construct只是普通的功能,而直接调用,而不是使用new ClassName;



文章来源: What happens if you call a constructor from a destructor?