Who cleans up, and when
Tray objects say when they are built and cleaned: one in a function that throws, two in the script.
<?phpclass Tray { public function __construct(public string $name) { echo "carry $name\n"; } public function __destruct() { echo "clean $this->name\n"; }}function serve(): void { $t = new Tray('soup'); throw new Exception('dropped');}try { serve();} catch (Exception $e) { echo "caught\n";}$a = new Tray('tea');$b = new Tray('cake');$b = null;echo "end of script\n";1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…
- 4…
- 5…
- 6…
- 7…
- 8…