Skip to main content
added 71 characters in body
Source Link
jimmy23013
  • 37k
  • 6
  • 77
  • 148

PHP

<?php

class a{
    function __destruct(){
        echo new Exception, "\n";
        global $x;
        $x = new a;
    }
}

$x=new a;

It is not a recursion. The destructor only calls the constructor of class a. The destructor itself is called after the destruction of the previous object. The stack always has the same size on each call of the destructor.

A more useful example:

<?php

class a{
    var $i;
    function __construct($i){
        echo "$i\n";
        $this->i = $i;
    }
    function __destruct(){
        global $x;
        if($this->i < 100)
            $x = new a($this->i + 1);
    }
}

$x=new a(1);

Count from 1 to 100.

PHP

<?php

class a{
    function __destruct(){
        echo new Exception, "\n";
        global $x;
        $x = new a;
    }
}

$x=new a;

It is not a recursion. The destructor only calls the constructor of class a. The destructor itself is called after the destruction of the previous object.

A more useful example:

<?php

class a{
    var $i;
    function __construct($i){
        echo "$i\n";
        $this->i = $i;
    }
    function __destruct(){
        global $x;
        if($this->i < 100)
            $x = new a($this->i + 1);
    }
}

$x=new a(1);

Count from 1 to 100.

PHP

<?php

class a{
    function __destruct(){
        echo new Exception, "\n";
        global $x;
        $x = new a;
    }
}

$x=new a;

It is not a recursion. The destructor only calls the constructor of class a. The destructor itself is called after the destruction of the previous object. The stack always has the same size on each call of the destructor.

A more useful example:

<?php

class a{
    var $i;
    function __construct($i){
        echo "$i\n";
        $this->i = $i;
    }
    function __destruct(){
        global $x;
        if($this->i < 100)
            $x = new a($this->i + 1);
    }
}

$x=new a(1);

Count from 1 to 100.

Source Link
jimmy23013
  • 37k
  • 6
  • 77
  • 148

PHP

<?php

class a{
    function __destruct(){
        echo new Exception, "\n";
        global $x;
        $x = new a;
    }
}

$x=new a;

It is not a recursion. The destructor only calls the constructor of class a. The destructor itself is called after the destruction of the previous object.

A more useful example:

<?php

class a{
    var $i;
    function __construct($i){
        echo "$i\n";
        $this->i = $i;
    }
    function __destruct(){
        global $x;
        if($this->i < 100)
            $x = new a($this->i + 1);
    }
}

$x=new a(1);

Count from 1 to 100.