I've read this answer:
Is destructor in PHP predictable?
But still fail to be 100% confident that the destructor is called as soon as the object goes out of scope.
My use case is the following:
class Transaction
{
private $isComplete = false;
public function commit() {
// ...
$this->isComplete = true;
}
public function rollBack() {
// ...
$this->isComplete = true;
}
public function __destruct() {
if (! $this->isComplete) {
$this->rollBack();
}
}
}
Say I'm using it this way:
function doSomething() {
$tx = $this->txManager->beginTransaction();
// ... code here may or may not throw an exception
$tx->commit();
}
Can I be 100% confident that in all cases (exception or not), the destructor will be the first thing that will be called as soon as the function ends?
My initial testing shows that yes, exception or not, the destructor is called right away. But I'd like a confirmation, and above all, a pointer to the relevant documentation.