Why, in this code
<?php class Orator { private $name; function __construct($name){ $this->name = $name; echo 'Π‘ΠΎΠ·Π΄Π°Π½ ΠΎΠ±ΡΠ΅ΠΊΡ ' . $this->name . '<br>'; } function __destruct() { echo 'Π£Π½ΠΈΡΡΠΎΠΆΠ΅Π½ ΠΎΠ±ΡΠ΅ΠΊΡ ' . $this->name . '<br>'; } } function outer(){ $obj = new Orator(__METHOD__); inner(); } function inner(){ $obj = new Orator(__METHOD__); echo 'ΠΠ½ΠΈΠΌΠ°Π½ΠΈΠ΅, Π²Π±ΡΠ°ΡΡΠ²Π°Π½ΠΈΠ΅!<br>'; throw new Exception('Hello'); } echo 'ΠΠ°ΡΠ°Π»ΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ<br>'; try { echo 'ΠΠ°ΡΠ°Π»ΠΎ try-Π±Π»ΠΎΠΊΠ°<br>'; outer(); echo 'ΠΠΎΠ½Π΅Ρ try-Π±Π»ΠΎΠΊΠ°<br>'; } catch (Exception $e) { echo 'ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅: ' . $e->getMessage() . '<br>'; } echo 'ΠΠΎΠ½Π΅Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ<br>'; the object is not immediately destroyed? After all, after the constructor, the destructor is immediately specified.
Now it works like this:
ΠΠ°ΡΠ°Π»ΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ ΠΠ°ΡΠ°Π»ΠΎ try-Π±Π»ΠΎΠΊΠ° Π‘ΠΎΠ·Π΄Π°Π½ ΠΎΠ±ΡΠ΅ΠΊΡ outer Π‘ΠΎΠ·Π΄Π°Π½ ΠΎΠ±ΡΠ΅ΠΊΡ inner ΠΠ½ΠΈΠΌΠ°Π½ΠΈΠ΅, Π²Π±ΡΠ°ΡΡΠ²Π°Π½ΠΈΠ΅! Π£Π½ΠΈΡΡΠΎΠΆΠ΅Π½ ΠΎΠ±ΡΠ΅ΠΊΡ inner Π£Π½ΠΈΡΡΠΎΠΆΠ΅Π½ ΠΎΠ±ΡΠ΅ΠΊΡ outer ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅: Hello ΠΠΎΠ½Π΅Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ And I wonder why it works wrong?
ΠΠ°ΡΠ°Π»ΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ ΠΠ°ΡΠ°Π»ΠΎ try-Π±Π»ΠΎΠΊΠ° Π‘ΠΎΠ·Π΄Π°Π½ ΠΎΠ±ΡΠ΅ΠΊΡ outer Π£Π½ΠΈΡΡΠΎΠΆΠ΅Π½ ΠΎΠ±ΡΠ΅ΠΊΡ outer Π‘ΠΎΠ·Π΄Π°Π½ ΠΎΠ±ΡΠ΅ΠΊΡ inner Π£Π½ΠΈΡΡΠΎΠΆΠ΅Π½ ΠΎΠ±ΡΠ΅ΠΊΡ inner ΠΠ½ΠΈΠΌΠ°Π½ΠΈΠ΅, Π²Π±ΡΠ°ΡΡΠ²Π°Π½ΠΈΠ΅! ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅: Hello ΠΠΎΠ½Π΅Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ
outercall occurs, then, in fact,outerhas not yet completed its work. Because There is a sequential processing of instructions .... And outer waits for completion in inner. As soon as the instructions passed to inner, the context returns to outer and only after that the object is destroyed .... if you putinner()afterouter()you will see that outer will die before the turn of the method ofinner()- Alexey ShimanskyΠΠ΅Π΄Ρ ΠΏΠΎΡΠ»Π΅ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡΠ°, ΡΡΠ°Π·Ρ ΡΠΊΠ°Π·Π°Π½ Π΄Π΅ΡΡΡΡΠΊΡΠΎΡ.- And it would be strange if the implementation of methods in a class depended on their consistent location within the class. Itβs just hard to imagine a class with 20 methods in which the higher the method, the first it works :)) I ranked the destructor second in order and you laugh at colleagues :)) - Alexey Shimansky