What does it do better than other languages? The article mentions features that sound like parity with other modern languages, but nothing that stands out.
What does it do better than other languages? The article mentions features that sound like parity with other modern languages, but nothing that stands out.
Shared nothing architecture. If you're using e.g. fastapi you can store some data in memory and that data will be available across requests, like so
import uvicorn, fastapi
app = fastapi.FastAPI()
counter = {"value": 0}
@app.post("/counter/increment")
async def increment_counter():
counter["value"] += 1
return {"counter": counter["value"]}
@app.post("/counter/decrement")
async def decrement_counter():
counter["value"] -= 1
return {"counter": counter["value"]}
@app.get("/counter")
async def get_counter():
return {"counter": counter["value"]}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9237)
This is often the fastest way to solve your immediate problem, at the cost of making everything harder to reason about. PHP persists nothing between requests, so all data that needs to persist between requests must be explicitly persisted to some specific external data store.Non-php toolchains, of course, offer the same upsides if you hold them right. PHP is harder to hold wrong in this particular way, though, and in my experience the upside of eliminating that class of bug is shockingly large compared to how rarely I naively would have expected to see it in codebases written by experienced devs.
Edit: Oh, you showed an example against Python! Now I get it!
1. Easy deployment - especially on shared hosting 2. Shared nothing between requests means easy concurrency AND parallelism 3. Mixing with HTML means you do not need a separate template language
Not everyone will see the third as an advantage, and many web frameworks, including PHP ones, prefer a separate, more restrictive, template language. It can be a footgun, but it is very convenient sometimes.
Like many other things, PHP makes it easier to do the wrong thing than other languages which make you do the same thing correctly.
However, even that doesn't handle in-flight requests that have their view of the files swapped out from under them. Yes, that's a small time window for an error to happen, but it's definitely not instantaneous.
The safer solution would be to update the server config to point at the new directory and reload the webserver, but now you're way past just uploading the new files.
I dont think its very different from changing proxy to point to different port.
$conn->query('SELECT * FROM giant_table ORDER BY foo LIMIT 1');
require 'old.php';
such that there's a significant interval between the request being spawned and it later including another file. The duration of the query is the opportunity for 'old.php' to go away, which would cause a 500 error.The difference is that you can have 2 ports listening at once and can close the first once it's drained of connections.
There's no fundamentally safe way to upgrade a bucket-of-files PHP app without tooling complex enough to rival another language's deployment.
In any case you would have to hit some few milisecond window in this opcache generation to break single request but even that might be unlikely thanks to how filesystems read files?
So if there's a 10 second gap between the start of execution and the 'require' line being reached and evaluated, then any incompatible changes to the file being required within that 10 seconds will cause an error.
With OpCache this could be solved so i guess lessin for me - deploy like this with opcache on.
I kid, I kid, but seriously, now you have a different set of issues.
But you are right there is no reason why you couldn't have two instances of the php app runing and switch between them. For some reason the PHP deployment services i've used seem to use the filesystem approach and i doubt it's laziness or incompetence.
And all that may be true for a trivial website. If you've written a personal project with 10,000 hits per year, YOLO. Go for it. The odds of it affecting one of those users is vanishingly tiny, and so what if it does? But if you're hosting something like a Wordpress site for a large company with lots of traffic, it's crucial to understand why "just rsync the files over" is not an acceptable deployment method.
I have a feeling you want to dunk on poor dumb PHP developer but like Forge is by the people who created Laravel. I believe they would put some thought into it. Maybe just maybe small chance of one bad request is not such a bad deal.
> Maybe just maybe small chance of one bad request is not such a bad deal.
If your company is OK with that, seriously, sincerely, right on! Keep doing this and move on to other problems.
If you have very long database query and you update your app in middle of it using blue-green load balancer you get to same production error. It is the same thing just implemented slightly differently because of PHP characteristics allow this and with different systems you have to use different strategy.
So yeah have good feeling about us PHP devs having bad deployment strategies.
Does that matter if a bit of downtime is acceptable?
They switched to blue/green deploys for the new site (which I suspect was done at the server level, not with symlinks or the like).
While I never actually wanted it, #2 was kinda cool spiritually. Same with CGI or a Cloudflare edge worker.
These days I imagine people are more likely to be using git pull or rsync anyway.
> And php-fpm being called by nginx is also more complicated than just "node script.js" or running a compiled Go binary.
Apache with mod_php is still an option AFAIK. It is also definitely easy to find everything pre-configured on share hosting. Then there is FrankenPHP.
Might not be the easiest option for everyone, but it is going to be for some people.
Besides the shared nothing architecture mentioned by sibling:
- A more mature community and ecosystem for open source packages e.g. basics like following semver
- One single clear option for package management, which is also by far best in class
- Simply better performance except maybe compared to javascript
While the rest of the options may tick one of the above boxes, none of them ticks all 3.
I still don't think PHP is a good idea for a greenfield project or anything, but they have done a good job of hiding all the footguns.
Agreed. I remember happily starting a couple of new PHP projects in the last decade and the frameworks felt like working in any other programming language.