Hello.

> I think there's a lot of confusion in this thread because different people 
> are talking about different scenarios. Perhaps it would be useful to 
> introduce some User Stories

Yesterday I briefly reviewed the WordPress code to estimate the
refactoring effort.
The amount of global state accessed through `global` and `static` is
quite large.
However, it seems technically possible to switch global and static
variables per coroutine.
I’m not sure how this will affect performance (maybe 1–2% on
switches?), but the task looks realistic.

At first I thought this was a bad approach, because if such a feature
were added to PHP,
everyone would start using `global` as a way to exchange data inside a
coroutine. However, this situation can be viewed from a completely
different perspective:

```php
function myFunction(): void {
    global $x;
}

// This code is equivalent to:
function myFunction(): void {
    effect ?string $x = null; // <-- function effect, external dependency
}

// This code is equivalent to:
function myFunction(): void in ?string $x {

}

// Then calling the function:
with ($x = "string") {
    myFunction();
}
```

In other words, this is a function effect that turns previously
unmanaged and unsafe code into a function with an explicit
external-dependency contract.
And in the future, it could be improved by adding proper effect
support to the language. It turns out that a feature previously seen
as an antipattern can actually be framed as a modern concept from
functional programming.

Of course, these statements need to be validated with code and testing,
and they also require a separate RFC.

Reply via email to