Hi Sammy,

On 19 June 2017 at 14:55, Sammy Kaye Powers <m...@sammyk.me> wrote:
> Hello internals!

Please could you add to the RFC a description of what a 'break' in a
retry block actually does? Although there is an example for it, and so
people can guess what it does, having it described clearly would be
better.

Could you also confirm, the number of retries can't be an expression,
right? i.e. the number of retries has to be hard-coded into the
script?

I don't believe the section "check out a full userland implementation
that covers all the features of the example above with a
fully-featured retry() function." is a valid comparison, and certainly
not how I (and I suspect other people) write re-attempt code.

For me it is much more normal to just use an in place
for($attempt.....) loop. A for-loop based equivalent of the example
from the RFC is below. Although it is a couple of lines longer than
the 'retry' version, it's not drastically longer. It also has the
benefit of not having a piece of data (the number of retry attempts)
be hard-coded into the PHP script that does the retrying.

> It forces the developer to make extra methods or functions,

tbh - that sounds like a good thing. One function to attempt the
thing, and another function to decide whether to retry or do something
else.

cheers
Dan



for ($attempt=0; ; $attempt++) {
    $id = 42;
    try {
        throw new RecoverableException("FAILED getting ID #{$id}");
        break; // obviously this won't be reached - but is here for completeness
    }
    catch (RecoverableException | AnotherRecoverableException $e) {
        if ($attempt > MAX_RETRY_ATTEMPTS) {
            echo $e->getMessage();
            throw $e;
        }
        if (42 === $e->getCode()) {
            throw $e;
        }
        echo "Failed getting ID #{$id} on try #{$attempt}. Retrying...";
        sleep(1);
    }
    catch (NonRecoverableException $e) {
        echo $e->getMessage();
        throw $e;
    }
}

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to