Earlier quoted context omitted.
> like they did with lambdas, that don't capture their environment https://www.php.net/manual/en/functions.arrow.php
Yes this exists, but again introduces a new thing into PHP, rather than using an existing thing. Instead of using existing function syntax and leaving away the name, now one has 2 more things: The "fn" (why can this not be "function" which already exists?) and in combination with "=>". So it is similar to "function ... use ..." which also has the need to introduce a new thing, the "use", instead of simply capturing i…
Exploring Coroutines in PHP
51–60 of 68 posts
Re: Exploring Coroutines in PHP
#52Earlier quoted context omitted.
I agree, but I would always pick a different language (like Go) for long running processes. PHP is great for shared-nothing apps though.
Don't worry, we're not comparing languages here. You are free to chose the language and community that fits best to your approach to software development.
Re: Exploring Coroutines in PHP
#53Just to clarify my intent with posts like this: I'm not suggesting that PHP is the ideal tool for every use case. The goal is to share a concept that might be unfamiliar to some developers, using PHP as the context. Sometimes learning about a concept in a familiar language helps you recognise where it might be useful elsewhere or apply it in a language that supports it better. Terms like coroutines, concurrency, prom…
The difficulty with these examples is that they are very different from the actual tasks that everyday web developers would like to parallelize, such as long-running database queries and API requests. These things often take orders of magnitude longer than any pure-PHP loop that a typical webapp might contain.
An example that fires off an async query and yields the result when it's ready will probably produce the right click in the minds of many more people. (mysqli can do this, but the interface is convoluted and badly in need of a Promise-like wrapper. I'm not sure if PDO/PostgreSQL even supports async queries.)
Re: Exploring Coroutines in PHP
#54Just to clarify my intent with posts like this: I'm not suggesting that PHP is the ideal tool for every use case. The goal is to share a concept that might be unfamiliar to some developers, using PHP as the context. Sometimes learning about a concept in a familiar language helps you recognise where it might be useful elsewhere or apply it in a language that supports it better. Terms like coroutines, concurrency, prom…
Contrived examples are okay if they help trigger a click in the reader's mind, "wow, I could use this to solve problem X in an actual project!" The difficulty with these examples is that they are very different from the actual tasks that everyday web developers would like to parallelize, such as long-running database queries and API requests. These things often take orders of magnitude longer than any pure-PHP loop t…
Re: Exploring Coroutines in PHP
#55Earlier quoted context omitted.
It's better to build your app in e.g. PHP, prove its worth, then identify the bottlenecks, THEN determine if you need multi-threading. And if so, determine if PHP would be the best language for it or if you'd be better off going for a different language - one with parallelism / multithreading built into its core architecture. The first logical step after PHP is NodeJS, which has the fast iteration cycles of PHP witho…
> The first logical step after PHP is NodeJS, which has the fast iteration cycles of PHP ... not really, you still have to deal with bundlers in real-world applications.
Re: Exploring Coroutines in PHP
#56Earlier quoted context omitted.
Fibers are incredibly powerful, as they can be used to implement seamless go-like concurrency with async, colorless functions. They were added to PHP by the maintainers of amphp ( https://amphp.org ), which is the best library for async PHP out there, providing a clean, object-oriented and async API for files, databases and everything that can make use of async I/O.
A fiber is a colored function. It's a different color than an async function, but it's not blindly swapable for a regular function.
Re: Exploring Coroutines in PHP
#57I think neither does PHP have the ecosystem for it, nor does it have the language primitives or standard library for it. Much cleaner languages than PHP already have enough issues getting concurrency things like fibers right. I don't see this ending in any implementation, that doesn't have surprising dysfunctional parts in it. Especially not, when again and again people behind PHP chose the easy way out for adding la…
PHP watched the absolute disaster of python 2 to 3 version upgrade and rightfully decided that it will be as much backwards compatible as possible. PHP 10 with a clean slate is a cute idea that would wreak havoc. Perl, or whatever it is called now, tried this and it was a total disaster.
Overall they actually did a pretty decent job to transition from Python 2 to 3. Many people didn't have any issues at all, and simply used the new thing instead of the old thing.
Python's language design in itself is debatable of course, but it certainly is better now than with Python 2.
Re: Exploring Coroutines in PHP
#58Earlier quoted context omitted.
PHP doesn’t force keys... You can omit the key and simply write `foreach($items as $value)`
BTW: https://www.php.net/manual/en/iterator.key.php It's literally in the interface.
> Interface for external iterators or objects that can be iterated themselves internally
Note “external iterators or objects”. The Iterator interface is not exactly everyday PHP, it’s a specialist utility for making classes iterable so they can be accessed like arrays. Most developers will rarely use it directly, and it’s not being used in the parent comment’s example either.
Iterating over something requires knowing where you are in the sequence, so of course you would need to implement a method to get the current position of the iteration.
Re: Exploring Coroutines in PHP
#59Earlier quoted context omitted.
PHP doesn’t force keys... You can omit the key and simply write `foreach($items as $value)`
What's idiomatic way to get index (0, n-1) with the value? Parent example shows you could not use $key as a generic solution.
``` foreach (new LimitIterator($generator, 0, 3) as $value) { echo $value; } ```
Re: Exploring Coroutines in PHP
#60Just to clarify my intent with posts like this: I'm not suggesting that PHP is the ideal tool for every use case. The goal is to share a concept that might be unfamiliar to some developers, using PHP as the context. Sometimes learning about a concept in a familiar language helps you recognise where it might be useful elsewhere or apply it in a language that supports it better. Terms like coroutines, concurrency, prom…
I haven't used it in ages,but I like PHP...