I
I
IgorNoskov2019-10-22 15:13:45
OOP
IgorNoskov, 2019-10-22 15:13:45

How to arrange calling methods one by one?

Hello! I am implementing a class that has one public method and the rest are hidden. Below is an example class structure. That is, in it, the public method calls the private methods one by one in a chain, but can also stop on any of the methods. Is this approach normal or is there a more elegant solution for such logic? Tell me please.

class Test
{
    public function start()
    {
        try {
            // do something...
            return $this->one();
        } catch (Throwable $exception) {
            // do something...
        }

        return false;
    }

    private function one()
    {
        try {
            // do something...
            return $this->two();
        } catch (Throwable $exception) {
            // do something...
        }

        return false;
    }

    private function two()
    {
        try {
            // do something...
            return $this->three();
        } catch (Throwable $exception) {
            // do something...
        }

        return false;
    }

    private function three()
    {
        try {
            // do something...
            return true;
        } catch (Throwable $exception) {
            // do something...
        }

        return false;
    }
}

$test   = new Test();
$result = $test->start();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2019-10-22
@IgorNoskov

Не знаю что конкретно вы делаете, но, возможно, вам нужна цепочка обязанностей.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question