I do not know if I can get the same result with the current PHP features
without taking many turns. So I thought of a feature that I'm initially
calling "interruptions" (similar to those that occur on a CPU).

Nowadays we have the Exceptions, which stop the execution of a function and
initiate a process of "catch" and treatment of the same. So I thought of
something similar, but did not break the execution flow, allowing reactions
to depend on what was happening inside a function.

It would basically work according to the following flow (the square
brackets number is the execution order):

function sum(int $a, int $b): int {
    [2] interrupts with new SumInterruption($a, $b);
    [5] return $a + $b;
}

interruptable {
  [6] $sum = [1] sum(1, 2);
  [7] printf($sum);
}
[3] catch (SumInterruption $interruption) {
  [4] printf('Calculating: %d + %d = ', $interruption->a, $interruption->b);
}

Using current PHP features I can do like that: https://pastebin.com/Bci6BBfi

Note that all code will be executed, and the interpection will only
redirect temporarily the execution flow to the "catch" block, then will
back to "sum()" block to return the sum. Like Exceptions, an Interruption
will traverse the code execution tree until find a interruption catch
block, but if it doesn't exists, just not will happen (or maybe throw
InterruptionNotHandledException or something like it).

In one of my real example cases, I have a code that could be manipulated by
another method. Currently I need argument the self instance to this method,
so it could run another method from the caller method to make some
adjustments, which is a bit confuses.

I hope you understand my point, and I am open to discuss that.

Thanks!

-- 
David Rodrigues

Reply via email to