What happens if you call a constructor from a destructor?

空扰寡人 提交于 2019-12-02 23:37:48

问题


Calling __construct() function from __destruct(),

<?php

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

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

?>

will it create infinite loop?


回答1:


No, but this will:

class Test {

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

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

}

new Test();

Example: http://ideone.com/94XUg




回答2:


No, it won't. __construct is just regular function while called directly instead of using new ClassName;



来源:https://stackoverflow.com/questions/10384836/what-happens-if-you-call-a-constructor-from-a-destructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!