PHP: Как правильно сделать эту «обертку» методами? - PullRequest
0 голосов
/ 11 декабря 2019

Мне нужно сделать что-то подобное, но избегать повторяющихся вызовов startTransaction и stopTransaction:

<?php 
//parent: let's supply transactions!
class MegaParent {

    public $__started;
    //code inside this method must be executed only once - at first call
    public function startTransaction()
    {
        if (!$this->__started) {
            echo "Transaction Started\n";
            $this->__started = true;
        }
    }
    //code inside this method must be executed only once - at last call
    public function stopTransaction()
    {
        if ($this->__started) {
            echo "Transaction Stopped\n";
            $this->__started = null;
        }
    }
}
//Child 1: I do something wrapped in transaction 
class ChildOne extends MegaParent {

    public function doer()
    {
        $this->startTransaction();
        echo "Doing ChildOne\n";
        $this->stopTransaction();
    }
}
//Child 2: I do something in transaction too but I need no nested transactions
class ChildTwo extends ChildOne {

    public function doer()
    {
        $this->startTransaction();
        parent::doer();
        parent::doer();
        echo "Doing ChildTwo\n";
        $this->stopTransaction();
    }
}

(new ChildTwo)->doer();

Результат:

Transaction Started
Doing ChildOne
Transaction Stopped
Transaction Started
Doing ChildOne
Transaction Stopped
Doing ChildTwo

Как получить такой результат:

Transaction Started
Doing ChildOne
Doing ChildOne
Doing ChildTwo
Transaction Stopped

?

...