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.