PHP 8.6 Closure Optimizations
Posted by moebrowne 3 days ago
Comments
Comment by kevincox 1 day ago
I don't know how strict JavaScript garbage collection rules are. This was non-observable for the longest time but FinalizationRegistry now exists which makes cleanup observable. It sounds like basically no guarantees are provided when an object will be cleaned up, so presumably an implementation would be allowed to make optimizations such are proposed where for PHP.
Comment by eurleif 22 hours ago
function foo() {
return function() { };
}
console.log(foo() === foo()); // This must log `false` in a compliant implementationComment by ragnese 19 hours ago
But, since when has PHP ever prioritized correctness or consistency over trivial convenience? (I know it's anti-cool these days to hate on PHP, but I work with PHP all the time and it's still a terrible language even in 2026)
Comment by tialaramex 15 hours ago
Comment by cardanome 14 hours ago
It has great web frameworks, a good gradual typing story and is the easiest language to deploy.
You can start with simple shared hosting, copy your files into the server and you are done. No docker, nothing.
Sure it has warts but so have all mainstream programming languages. I find it more pleasant than TypeScript which suffers from long compile times and a crazy complex type system.
The only downside is that PHP as a job means lots of legacy code. It a solid career but you will rarely if ever have interesting programming projects.
Comment by phplovesong 17 hours ago
Comment by hajile 10 hours ago
Comment by user3939382 7 hours ago
> `new MyClass() === new MyClass()`
Does that look like the code you’re writing for some reason? Because I’ve seen 100k loc enterprise PHP apps that not once ran into that as an issue. The closest would be entities in an ORM for which there were other features for this anyway.
Comment by philo23 20 hours ago
Comment by moebrowne 3 days ago
> Stateless closures, i.e. those that are static, don't capture any variables and don't declare any static variables, are cached between uses.
Comment by hyperionultra 1 day ago
Comment by rererereferred 1 day ago
Comment by ethan_smith 19 hours ago
Comment by typia 22 hours ago
Comment by Gormo 22 hours ago
PHP has a vastly simpler toolchain (making it much more effective for rapid iteration), much more consistent and well-thought-out syntax, a more extensive standard library, type safety without having to transpile code from another language (so no build processes that rival C++ in complexity just to still have interpreted code at the end), native and full-featured object orientation, generally better runtime performance, and a package ecosystem with Composer that isn't overrun with inane vanity projects and supply-chain vulnerabilities.
The only major downside to PHP is that it's not great at multithreading, but if you're building microservices where parallelization is handled by an external orchestrator, then you can design around that pretty effectively.
Comment by bakkoting 12 hours ago
All major server-side JS runtimes are capable of executing TypeScript without transpilation these days. Complex build processes are only really a thing for client-side JS. The normal state of affairs these days is to run server-side code with no build step whatsoever.
Comment by Gormo 11 hours ago
Comment by dgb23 19 hours ago
Comment by Meekro 19 hours ago
Comment by toast0 16 hours ago
Sure, in PHP, the reality is that after your request is processed, all the state is garbage and is thrown out. But once you embrace that reality and stop trying to make sculpture from garbage, you can make some pretty damn fast pages that get straight to the point. Of course, a lot of people look at my fast PHP and say that it too is garbage, but at least it's fast garbage :P
Comment by 9dev 7 hours ago
Comment by g8oz 19 hours ago
That being said there are very decent options for long running processes/application servers these days - see RoadRunner, Swoole and Frankenphp.
Comment by troupo 16 hours ago
Where "application" is basically a single page with less code than a typical React page. Even 20 years ago you'd run into DB struggling to give you data fast enough before you hit any issues with the "re-running the entire app".
And you have to screw your database really badly to see any issues early. Hell, phpBB was horrendously bad, running dozens of heavy DB queries on each page, and was still powering some of the internet's busiest forums.
> Now there are a whole bunch of tricks both in the language and with tooling to alleviate that, but still it's inherently there. It's an advantage for other reasons though.
Yes. It is an enormous advantage: it's fire and forget. You don't need to "SSR" your app (getting all data and state), ship it to the client with a bundle, then "re-hydrate" it (once again pulling data and state from the server) etc.
Comment by Meekro 19 hours ago
On the other hand, Javascript's parallelization is one of the hardest-to-understand things I've ever seen: in order to really grasp the async/await model, you have to know the whole story of callback hell and how modern Javascript papers over it while carefully preserving backwards compatibility.
Comment by sidkshatriya 18 hours ago
No actually it's a joy to have no multithreading. It keeps the complexity budget lower. You can do multithreading through a custom PHP module if you had a very specific need. Maybe my requirements were too simple but I've never really felt the need. The shared nothing PHP architecture really helps you get away with this.
Anyways as the parent comment said:
> but if you're building microservices where parallelization is handled by an external orchestrator, then you can design around that pretty effectively.
Comment by martinald 14 hours ago
What if you need to call multiple external APIs at once with complex json? Sure you can call them one after another, but if each take (say) 2s to return (not uncommon IME), then you are in real trouble with only one thread - even if it is just for one "request".
I guess I'm spoilt in .NET with Task.WhenAll which makes it trivial to do this kind of stuff.
Comment by theodorejb 14 hours ago
https://www.php.net/manual/en/function.curl-multi-exec.php
https://docs.guzzlephp.org/en/stable/quickstart.html#concurr...
Comment by Gormo 11 hours ago
Comment by Gormo 11 hours ago
A few years ago, I had a PHP project that had grown by accretion from taking a single complex input and triggering 2-3 external endpoints to eventually making calls to about 15 sequentially. Processing a single submission went from taking 5-10 seconds to over five minutes.
This was readily solved by moving to ReactPHP (https://reactphp.org/), which implements async via event loops. I was able to reduce the 15 sequential external API calls to four sequential loops (which was the minimum number due to path dependencies in the sequence of operations). That reduced the five minutes back to an average of 20-30 seconds for the complete process.
It wasn't using true multithreading, but in a situation where most of the time was just waiting for responses from remote servers, an event loop solution is usually more than sufficient.
Comment by mschuster91 18 hours ago
That's... not necessarily a bad thing to lack. Entire classes of bugs that are common in Java, C/C++, .NET and other true multi-threaded environments simply cannot exist in the PHP world at all.
Comment by sellmesoap 9 hours ago
Comment by Tadpole9181 20 hours ago
> PHP has a vastly simpler toolchain
Firmly disagree.
You can install Node and have a basic server running in a few seconds.
PHP requires installing and setting up a server tied into FPM and then reconfiguring a slurry of bad defaults. If you don't avoid the footgun of "traditional" deployments, you get to deal with mixed versions of source. If you don't avoid the footgun of "native" sessions, you get to deal with INCOMPLETE_CLASS errors galore.
And if you want a dynamic frontend, you're still going to want to bust out JS.
> I can't find any reason why I wouldn't use PHP instead
Using a single language for both frontend and backend with (largely) the same availability of tooling and the ability to share code (i.e. type definitions).
> generally better runtime performance
I find this hard to believe? Intuitively, I would assume that the Node / Bun engines are significantly faster than PHP - which doesn't even come with it's JIT enabled by default on the (perfectly valid) note that backends are almost always constrained by DB times.
> a package ecosystem with Composer that isn't overrun with inane vanity projects and supply-chain vulnerabilities.
Functionally, Composer is worse than any of the NPM package managers IMO. PHP's global, shared namespaces preventing monkey patching and nested dependencies is a huge burden when you need to use Lib A and Lib B, but both have conflicting dependencies on Lib C.
But the only reason it doesn't suffer (as many) supply chain issues is two-fold:
1. Packagist's source of truth is the repo and tags. It's much easier to notice a Github account being compromised, which is already harder because it's always had better account security expectations, than NPM. But this comes at costs - such as being unable to rename a package in-place, because Composer gets really confused when it clones a trunk that has a different name than what you asked for. And it's not intrinsically more secure, since tags are not immutable and people can host on less secure VCS platforms.
2. But more than that... it's just less used? The PHP ecosystem is noticeably smaller and has way less happening.
So its very much trade-offs.
Comment by runjake 20 hours ago
Without mentioning more, the PHP equivalent to your Node example is `php -S`.
Comment by chuckadams 18 hours ago
Comment by Gormo 11 hours ago
Launch the interpreter's built-in dev server in your project directory, load up localhost in your browser, work on your code, and testing incremental changes locally is just a matter of hitting F5.
Comment by archerx 9 hours ago
“Using a single language for both frontend and backend with (largely) the same availability of tooling and the ability to share code”
Is a negative I went backend world and front end world to be different because they do very very different things.
“But more than that... it's just less used? The PHP ecosystem is noticeably smaller and has way less happening.”
That’s not true, PHP is less resume driven development and actually about productivity. I’m really happy narcissist hate PHP and don’t burden it with their garbage and slopworks.
Comment by klaussilveira 21 hours ago
- Batteries included, everything you need out of the box. No need to npm install left-pad
- Optional, per-file strict types, without the need to transpile from another language
- No need to build anything, lightning fast scripting language
- Is its own HTML templating language (or XML), no need for JSX
- No supply-chain fiascos, stable and mature ecosystem
Comment by hparadiz 20 hours ago
Comment by jamesfinlayson 9 hours ago
Comment by pluc 22 hours ago
Comment by phplovesong 17 hours ago
Comment by konfusinomicon 15 hours ago
Comment by tail_exchange 17 hours ago