The code below illustrates the destruct() being called twice. I'd like to know why?
class A {
function hi(){ echo 'hi'; }
function __destruct(){
echo 'destroy';
}
}
class B{
public $this_ = '';
function __construct(){
$this->this_ = new A;
}
function __call($method, $params) {
return call_user_func_array(array($this->this_, $method), $params);
}
}
$b = new B;
$b->__destruct();
output:
destroydestroy
EDIT
Both zneak and TomcatExodus is correct. If I simply:
[..code..]
$b = new B;
$b->__destruct();
print 'end of script';
The output will show:
destroyend of scriptdestroy
Bcreates an instance of classA, andBalso uses__call()to route to the self containedAobject, a__destruct()call onBgets routed to the self contained instance ofA. At script termination, all objects leave memory, and theAobject fires destruct again. Our answers were still right I believe, just with a twist on the situation.