it became interesting to me when _destruct is called: after deleting an object or before? Do you want to know if you can get the values ​​of the properties of the object that we are going to delete inside the destructor and the properties of other objects? Small code that does not work:

class Boo{ public $ty=10; function __destruct(){ global $obj2; echo $this->ty.$obj2->grr; } } class Secon{ public $grr=9; } $obj2=new Secon; $obj=new Boo; unset($obj); 
  • Is the $ obj2 global variable that you work with it in a function? The destructor is executed before deleting; you can work with variables of the class object to be deleted. Perhaps I am mistaken, but the code does not work for me when I declare a variable and immediately assign it a value in the class description. - kingNothing
  • Thanks for the hint with global visibility. - koza4ok
  • one
    Knowledgeable people also say that it is necessary to physically destroy all class variables If you use the script. For that you can use the following script to clean up the class. <? php public function __destruct () {foreach ($ this as $ key => $ value) {unset ($ this -> $ key); }}?> - Photon
  • After all, you have a version of php> 5? (just in case) - Sergiks
  • DA.Already works when he added the flag global + mystic Denver. - koza4ok

1 answer 1

Your code works for me.

__destruct () is called when the object has no references left, or during the normal completion of the script. Yes, all methods and class properties are available to it.

  • "or during the normal completion of the script." - Anyway, it will be executed when the creak is finished? - koza4ok
  • one
    If Fatal crashes, or the class has not been instantiated, it will not be executed. - Sergiks
  • all thanks ... - koza4ok