3 ms·
I have read much about Fibers since they were introduced and have never come up with a real use case. All the examples I find are like the trivial ones here wh
by donatj 1y ago
I have read much about Fibers since they were introduced and have never come up with a real use case.
All the examples I find are like the trivial ones here where it just feels like instead of jamming a bunch of code into a single messy function that yields, you'd be better off particularly from a static analysis standpoint just having ordered method calls or chained callables where each step returns the next step as a callable.
I've yet to see a use case where I can't come up with a safer way to do it without Fibers, but I would love if someone could enlighten me because I feel like I am absolutely missing something.
- Pesthuf 1y agoI once had a use case where I wrapped a callback based library (Amazon's S3 library I think) call to instead return an iterator. Couldn't do that with only a generator since you can't yield in the callback itself. And it wasnt possible to write a custom iterator class since the library didn't give you back control until it was done. That was the first and only time they were kinda useful to me.
- danogentili 1y agoFibers 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 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.
- donatj 1y agoA fiber is a colored function. It's a different color than an async function, but it's not blindly swapable for a regular function.
- danogentili 1y agoAsync functions based on PHP fibers are explicitly uncolored, they do not require any special keywords to be invoked, and are explicitly swappable with any regular functions.
- hibikir 1y agoI have a service using them a lot. It ultimately relies on a couple dozen downstream dba and endpoints, with some calls requiring strict dependencies, and others running in parallel. So I just draw the dependency chart among the functions, and let the service manage all the parallelism and asynchronous bits with minimal babysitting. If it was all chained, then sure, it's not a significant gain
- bornfreddy 1y agoI think you're not missing anything (or at least, I'm missing it too - not much advantage over yield). Generators rock though.