frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Exploring Coroutines in PHP

https://doeken.org/blog/coroutines-in-php
64•doekenorg•3d ago

Comments

Amakanski•3h ago
>> This would require a yield to syntax, which doesn't exist.

There is however the 'yield from' statement.

zelphirkalt•2h ago
This means, that most likely the keyword will not be "yield". PHP is not known for keeping a low number of keywords. Rather they introduce new ones, to tack on things to the language, like one can see in the implementation of anonymous functions, where one needs to use "use" explicitly, instead of the language having the semantics that most other languages have for lambdas. Which immediately turns in to a source for nullability errors.
djxfade•2h ago
That's not totally correct. PHP has a "short function" syntax for that specific use case, it automatically captures data without the 'use' statement.

    $a = 5;
    $b = fn ($c) => $a * $b;
    print($b(2)); // 10
    print($b(4)); // 20
Cyykratahk•2h ago
I'd use `yield from` more if I didn't have to re-index arrays before yielding from them:

    function numbers() {
      yield from [1,2,3];
      yield from [4,5,6];
    };
    
    foreach (numbers() as $k => $v) { echo "$k => $v\n"; }
    0 => 1
    1 => 2
    2 => 3
    0 => 4
    1 => 5
    2 => 6
bvrmn•52m ago
PHP generators/iterators is a hot mess. I don't know any other language which forces iterators to output a key for `foreach(... as $key => ...)`.
jw1224•38m ago
PHP doesn’t force keys... You can omit the key and simply write `foreach($items as $value)`
pachico•3h ago
I don't write PHP code anymore. I had a great time doing so for years but now I mostly write in Go for a company that writes a lot in PHP.

What I see from PHP is a missed opportunity for not having any native lightweight multi thread capabilities not a robust HTTP server.

I wish the situation changed.

beberlei•3h ago
My hope is that the parallel extension will get more widespread adoption when its integrated into FrankenPHP: https://github.com/krakjoe/parallel
9dev•2h ago
I still have flashbacks from working with the pthreads extension, which caused extremely hard to debug, non-reproducible segfaults sometimes; I realise Joe has probably started from scratch and improved a lot on that (and I know he's a generally awesome guy), but without a properly financed maintainer team to support him, I'm not sure I want to take that risk again before parallel has gained some maturity.
cardanome•3h ago
I work daily with PHP and honestly nearly all my code I write is synchronous.

The shared-nothing architecture of PHP makes that really a non-issue for me. Requests never share any state with each other. Something like RabbitMQ can handle communication between systems.

zelphirkalt•2h ago
That's in no way lightweight though, and most languages can easily do the same. Just launch multiple instances/VMs/processes. That's having multiple separate OS processes, each having everything that is needed to run PHP, and having no way to communicate with each other, other than what you implement. No channels, no task distribution, no queue on which to sync and take tasks from, no signaling of all processes being done and then accumulating the results. That is why you then need something like RabbitMQ or other things, and it does not mitigate the heaviness of the approach.

It is kinda funny, that you mention RabbitMQ, which is written in Erlang, which is famous for its lightweight processes. But also compare the approach with thread pools built into the standard libraries in other languages. And even many of those are heavy weight compared to Erlang's lightweight processes.

ckbkr10•2h ago
PHP is not about lightweight, it's about rapid development. A lot of implementations in PHP are dead simple and can be debugged even by beginners.

You generally do not implement efficient systems in php, they are easy to debug, fast to code and quick to fix though.

Klathmon•1h ago
PHP's approach is simple though, and in my experience that simplicity pays off when you do start scaling the systems.

In other systems once you get beyond a single machine you need that external communication mechanism anyway, and now you have multiple classes of comms which introduces bugs and complexity and performance cliffs.

In PHP you just throw another server at it, it'll act the same as if you just added another process. Nice linear scaling and simple to understand architectures.

Man I miss working in PHP

Cthulhu_•1h ago
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 without the low-level memory management or the enterprisey application server headaches of other languages, AND it has the advantages of a persistent service without needing to worry too much about parallelism because it's still single process.

But if you still need more performance you have a few options. But if you're at that point, you're already lucky. Most people wish they needed the performance or throughput of a language/environment like Go.

saghm•46m ago
By this logic, it seems like it would make more sense to just start with Node rather than PHP for the prototype and save the potential rewrite. Node does seem more popular than PHP nowadays to me as an outsider to both, so maybe that's exactly what did happen.
pier25•1h ago
The execution model is definitely the biggest issue with PHP these days.

Although now that the PHP Foundation is officially supporting FrankenPHP maybe things will be evolving into a new paradigm.

https://thephp.foundation/blog/2025/05/15/frankenphp/

todotask2•1h ago
There was a long discussion recently on FrankenPHP.

https://www.reddit.com/r/PHP/comments/1lqpkfq/frankenphp_any...

donatj•2h 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•1h ago
I 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•1h ago
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.

donatj•43m ago
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.
hibikir•1h ago
I 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
zelphirkalt•2h ago
I 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 language features, like they did with lambdas, that don't capture their environment, unless you explicitly state what they capture.

Let alone the standard library being an unfixable mess, if they want to pursue backwards compatibility. What they would need to do is a clean break. Something like "PHP 10 is a breaking change, we are modernizing the base language and standard library and getting rid of decades of cruft." -- Which then many PHP users would hate.

No, PHP is a not a language whose design has what it takes. A library that claims to have such advanced stuff implemented in PHP is not to be easily trusted to be free of hassle and tripwire, because the language itself has those built-in. For example with Fibers, one has to manually call suspend and resume. Other languages get fibers done without this hassle.

EDIT: For all the downvoters, who don't care to actually give reasons or discuss, the cracks are already showing in the Fibers manual itself. Look at the first comment for example: https://www.php.net/manual/en/language.fibers.php#127282 which links to a bug/issue in the Fibers repo. It is a bug, even recognized via issue label, it is verified, as recognized via issue label, and it is ... closed. Apparently it will not be fixed, and has been simply closed.

supriyo-biswas•2h ago
> like they did with lambdas, that don't capture their environment

https://www.php.net/manual/en/functions.arrow.php

zelphirkalt•1h ago
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 its environment.

The point is, that the designers of PHP decide again and again to add another wart, rather than designing the language in an extensible way. Here a little extra, there a little extra, here a new keyword, there a new thingy. Compare that with other languages. Even JavaScript has managed to have simply the same syntax for functions and anonymous functions. Although it did also introduce an arrow syntax. However it's arrow syntax is not really needed, even though shorter, in contrast to PHP, where you need it, unless you want to list all the parts of the environment you need in the anonymous function. At least in JS they didn't introduce a new extra thing like "fn".

I don't think the design of PHP is done with good oversight. It is done with the approach of tacking things onto what exists, in the easiest way they are able to do, without any goal of keeping the language concepts and keywords minimal. Designing something like "fn" or "use" is a hack to make implementation easier, because they are previously unused keywords, that will then make it easier to adapt the parser for the language, but these decisions are offloading mental load to the user, which ultimately is a bad design.

conradfr•2h ago
The standard lib thing is overblown. What exactly is bothering you?

And PHP has deprecated a lot of things along the way.

philo23•1h ago
Personally I've never come across any problem in PHP that I feel like Fibers would help me solve.

Having Fibers in PHP is a nice addition but it definitely feels more like plumbing for other PHP extensions/frameworks to use, rather than something the average dev would use themselves directly day to day.

jerf•58m ago
Of course you haven't come across any such problem in PHP, because up until this point, if you had, you would have had to switch to another language to solve the problem that required the feature PHP didn't have.

There's an evaporative cooling effect on languages that don't have certain features where all the people who need those features leave after some number of years of not having them, leaving behind only the people who don't need them. There's a survivorship bias as a result.

I worked with dynamic scripting languages primarily for the first 15 years of my career but the definitive split for me was precisely when I had a problem they couldn't solve because they couldn't handle tens of thousands of simultaneous connections in any reasonable way.

This reply applies to all the commenters posting "but I've never needed this".

That doesn't mean PHP was useless without this feature, as it observably has solved a lot of problems. It just means that you shouldn't draw out too many conclusions from "I've never needed it", because you're still using it precisely because you're working in a space that doesn't need it, and your community is not constantly complaining about it because the people who would be complaining are no longer in your community. It means if you do develop a need for it in the future you won't have to leave PHP.

As a result this is a bad metric to measure the utility of a feature with.

On the flip side, it is valid to say "we've come this far without it and maybe we should be focusing on what we can do and continuing to invest in making that better rather than chasing the things we can't", especially since features like adding true threading add a lot of constraints to an implementation and make everything else you ever do in that implementation more expensive. Personally I am of the opinion that all of the dynamic scripting languages really need to just settle down, accept that they are what they are, and stop adding feature after feature after feature to 25-30 year-old languages.

Show HN: OffChess – Offline chess puzzles app

https://offchess.com
174•avadhesh18•4h ago•59 comments

Reflections on 2 years of CPython's JIT Compiler: The good, the bad, the ugly

https://fidget-spinner.github.io/posts/jit-reflections.html
20•bratao•2d ago•6 comments

WebAssembly: Yes, but for What?

https://queue.acm.org/detail.cfm?id=3746171
31•todsacerdoti•5h ago•27 comments

New sphere-packing record stems from an unexpected source

https://www.quantamagazine.org/new-sphere-packing-record-stems-from-an-unexpected-source-20250707/
358•pseudolus•19h ago•175 comments

Attimet (YC F24) – Quant Trading Research Lab – Is Hiring Founding Researcher

https://www.ycombinator.com/companies/attimet/jobs/6LaQIc5-founding-researcher-quant
1•kbanothu•1h ago

Epanet-JS

https://macwright.com/2025/07/03/epanet-placemark
148•surprisetalk•3d ago•16 comments

Berry Script: lightweight embedded scripting language for microcontrollers

https://berry-lang.github.io/
52•hasheddan•3d ago•16 comments

Mercury: Ultra-fast language models based on diffusion

https://arxiv.org/abs/2506.17298
493•PaulHoule•1d ago•209 comments

I used o3 to profile myself from my saved Pocket links

https://noperator.dev/posts/o3-pocket-profile/
424•noperator•1d ago•163 comments

What Microchip doesn't (officially) tell you about the VSC8512

https://serd.es/2025/07/04/Switch-project-pt3.html
136•ahlCVA•3d ago•44 comments

Launch HN: Morph (YC S23) – Apply AI code edits at 4,500 tokens/sec

205•bhaktatejas922•23h ago•157 comments

LookingGlass: Generative Anamorphoses via Laplacian Pyramid Warping

https://studios.disneyresearch.com/2025/06/09/lookingglass-generative-anamorphoses-via-laplacian-pyramid-warping/
104•jw1224•15h ago•21 comments

Exploring Coroutines in PHP

https://doeken.org/blog/coroutines-in-php
64•doekenorg•3d ago•28 comments

Can an email go 500 miles in 2025?

https://flak.tedunangst.com/post/can-an-email-go-500-miles-in-2025
8•zdw•3d ago•0 comments

The chemical secrets that help keep honey fresh for so long

https://www.bbc.com/future/article/20250701-the-chemical-secrets-that-help-keep-honey-fresh-for-so-long
182•bookofjoe•4d ago•121 comments

The Miyawaki Method of micro-forestry

https://www.futureecologies.net/listen/fe-6-5-the-method
185•zeristor•3d ago•36 comments

Adding a feature because ChatGPT incorrectly thinks it exists

https://www.holovaty.com/writing/chatgpt-fake-feature/
1021•adrianh•22h ago•370 comments

SIMD.info – Reference tool for C intrinsics of all major SIMD engines

https://simd.info/
43•pabs3•12h ago•8 comments

When Figma starts designing us

https://designsystems.international/ideas/when-figma-starts-designing-us/
260•bravomartin•2d ago•117 comments

Why are there no good dinosaur films?

https://briannazigler.substack.com/p/why-are-there-no-good-dinosaur-films
125•fremden•3d ago•297 comments

François Chollet: The Arc Prize and How We Get to AGI [video]

https://www.youtube.com/watch?v=5QcCeSsNRks
203•sandslash•4d ago•176 comments

Apple just released a weirdly interesting coding language model

https://9to5mac.com/2025/07/04/apple-just-released-a-weirdly-interesting-coding-language-model/
91•ksec•3d ago•32 comments

What is going on in Unix with errno's limited nature

https://utcc.utoronto.ca/~cks/space/blog/unix/ErrnoWhySoLimited
54•ingve•4d ago•33 comments

Lightfastness Testing of Colored Pencils

https://sarahrenaeclark.com/lightfast-testing-pencils/
169•picture•3d ago•43 comments

# [derive(Clone)] Is Broken

https://rgbcu.be/blog/derive-broken/
111•RGBCube•3d ago•81 comments

Show HN: NYC Subway Simulator and Route Designer

https://buildmytransit.nyc
173•HeavenFox•23h ago•26 comments

CU Randomness Beacon

https://random.colorado.edu/
39•wello•3d ago•12 comments

The Two Towers MUD

https://t2tmud.org/
127•astronads•2d ago•86 comments

Solving Wordle with uv's dependency resolver

https://mildbyte.xyz/blog/solving-wordle-with-uv-dependency-resolver/
183•mildbyte•2d ago•15 comments

My first verified imperative program

https://markushimmel.de/blog/my-first-verified-imperative-program/
162•TwoFx•19h ago•74 comments