destructor sequence explained

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello
Can someone please help explain how the order of object destructors called
at shutdown is determined, especially with regards to objects composed
of other objects?

For example in the code below, the first example calls the destructors
exactly in the order I would expect. Since object B is still
referenced by object A, A is destroyed first, then B. However in the
second example, it appears that object B is destroyed before object A,
even though A still references B. Can someone please shed some light
on this? Thanks.
James

<?php


class A{

  public function __construct($b){
      $this->b = $b;

  }

  public function __destruct(){
      echo __class__ . " destroyed\n";
  }
}
class B{
  public function __destruct(){
      echo __class__ . " destroyed\n";
  }
}

$b = new B();
$a = new A($b);


?>


Outputs:
A destroyed
B destroyed



However if I add the


class A{

  public function __construct($b){
      $this->b = $b;
      $this->myself = $this;    // <--- This is the only change
  }

  public function __destruct(){
      echo __class__ . " destroyed\n";
  }
}
class B{
  public function __destruct(){
      echo __class__ . " destroyed\n";
  }
}

$b = new B();
$a = new A($b);


?>

That outputs:
B destroyed
A destroyed

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux