What happens if you call a constructor from a dest

2019-03-07 04:31发布

问题:

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;