by mojuba · 4y ago · 0 comments
PHP is not a likable language but it's probably not going away any time soon. Its concept of "one endpoint is one script" is one of its biggest killer features that no other language has been able to deploy in such an accessible manner. Well, Perl and old-style CGI aside, of course.
That, and also the fact that it may be pretty much the only dynamic language in use today with ARC and not a GC. Funny how PHP devs are often unaware of this.
Anyway I wish the language at least evolved faster and we wouldn't wait for 8.0 to have global `const` definitions instead of the archaic `define()` for example.
I don't see that as its biggest killer feature. In fact most projects built on PHP using a framework won't use the feature, as every request will be routed through a front controller. This has been true for at least a decade.
I think the killer feature of PHP is its shared-nothing architecture, tear-down at every request (or every x requests if you're using php-fpm) which has proved remarkably successful as it is so resilient.
I agree. Shared nothing is definitely the killer feature.
Even if php-fpm is tear down at every x requests, each request still starts afresh in which each needed PHP file is required and loaded again. This is not slow because of opcode caching means that parsing etc. does not need to be done afresh. PHP just pulls from the opcode cache. At the end of the request the request is torn down. The interpreter instance might be reused again for performance (though that may be torn down every x request also as you implied with php-fpm for instance).
When you want to find out why something did not work in PHP you just need to go back to the start of the request and work your way from here. With a stateful system like Django (Python) or nodejs (JavaScript) you need to worry about the state of the full server in addition to what happen in a specific request.
This is an ideal model of course because large PHP implementations uses caching (in things like redis) to speed things up and the cached state could be the one introducing a bug. However, in practice, PHP tries to put in most of the state in a relational db so debugging a PHP script is much simpler due to this shared nothing feature.
I see the same pain for some next.js devs these days, even though it does allow parameters in roues in [name].js file form, I saw some people generating routers from file trees and finding it an acceptable compromise. Well, each to their own, I guess.
> Every web request starts from a completely blank slate. Its namespace and globals are uninitialized, except for the standard globals, functions and classes that provide primitive functionality and life support. By starting each request from a known state, we get a kind of organic fault isolation; if request t encounters a software defect and fails, this bug does not directly interfere with the execution of subsequent request t+1.
> An individual web request runs in a single PHP thread. This seems at first like a silly limitation. But since your program executes in the context of a web server, we have a natural source of concurrency available: web requests. Asynchronously curl’ing to localhost (or even another web server) provides a shared-nothing, copy-in/copy-out way of exploiting parallelism. In practice, this is safer and more resilient to error than the locks-and-shared-state approach that most other general-purpose languages provide.
Then came along templating engines like Smarty, and they introduced a new templating language for a language that was already built for templating. For some reason, "<?= $foo ?>" was considered much worse than "{$foo}".
smarty.net has a pamphlet up there where pros/cons (but actually only pros) are discussed [1]. Failing to understand that "<?php ...>" syntax is a SGML processing instruction with well-defined ways to embed into a hosting markup language like HTML, they go even so far as to say
> Another issue with PHP tags is that they share the <> characters of HTML tags. In the application code this isn't an issue, but mixed with HTML, <?php ?> tags are a maddening process to tell them apart from the HTML tags. This also blurs the line between application and presentation separation since any PHP logic can be injected into a template.
Yeah no, now you need new escaping syntax if you ever wanted to have verbatim {$whatever} in your HTML.
Notably missing is a discussion about HTML-aware, injection-free templating such as for preventing {$bla} expanding into <script>alert('Pwnd')</script> when sourced from user input, which is what SGML and other HTML-aware template engines can do, and which in the name of everything that's holy should've been implemented in PHP 2 to save the web from becoming a botnet.
Hack did this correctly with XHP templating. Unfortunately, Hack was killed by PHP7, and of all the features it took from Hack, context-aware native templating was not among them.
A stigma that remains to this day, even though the spec is very clear about those kinds of short tags and the flags that affect them. PHP developers are absolutely terrified that some imaginary environment flag they have control over is going to brick their application, but are happy to bring in an entirely new templating language as a dependency instead. I'm sure Smarty was borne of a need, but I was never sure what it brought to the table except different tags for existing features.
Is that still a thing in PHP world? I recently ended up working on a 'modern' PHP project using Laravel and it was all routers and views and models and auto-generated scaffolding code, just like Rails and Django. There was no sign left of that 'old' approach.
I'm of the strong opinion that modern PHP with frameworks and auto-include and a ton of modelling is the wrong way to do it, but everyone else can do it that way, and I can still do things the old way and I can be happy.
PHP has this amazingly zen model of computing. At the end of the request, everything is thrown away to start fresh. This means anything you don't output is garbage. I don't think it makes a lot of sense to make sculptural models of your data and page structure only to throw them away. It's best to keep things simple.
There's some times and places where it makes sense to use more features, but starting with a kitchen sink framework makes it very hard to get simple where it's needed, but you can always pull in a little bit of something more complex into a simple thing.
Over Django: - Because if not doing SPAs, it provides infinitely better tools for the frontend, such as an integrated assets pipeline, a good template language with componetisation support - Because it is faster - Because during development every change is not a restart - Because it has an integrated queue system - Because you have popular frontend solutions such as Livewire
The only better thing I've found in Django is the admin.
Over Rails: - Because it is faster - Because PHP is far, far, far more popular tan Ruby - Because it has integrated authentication - Because it is easier to hire for
just some reasons from the top of my mind. I don't have a ton of experience with Rails and I think it is still great, but IMHO Laravel is better. I have worked with Django for about 10 years, and other than the Admin, I don't miss anything when working with Laravel. Language wise PHP vs Python is just syntax, I don't care as both ecosystems are pretty healthy.
I had to learn JEE(JSP,JSF,CDi,JPA) for some project lately, so now I understand a bit more why Doctrine, Synfony and co are the way they are, coming from Go, Ruby, Python and JS... Well I prefer just writing some JEE stuff rather than using a PHP framework in that case, because I realized the Java cargo culting was just insane in the PHP community and how PHP is poorly equipped to replicate that JEE stack...
PHP isn't going away though, billions of PHP scripts out there to maintain, and CMS are built in PHP mostly because of hosting providers still defaulting to Apache servers because cost.
Nothing is too little too late though, when I read about the evolution of Java, I realize I'm lucky to suffer through it today rather than 15 years ago...
> Its concept of "one endpoint is one script" is one of its biggest killer features that no other language has been able to deploy in such an accessible manner. Well, Perl and old-style CGI aside, of course.
JSP on Tomcat works mostly the same way. and nothing prevents anybody from using python or ruby, (or C++) in FastCGI mode. PHP has that whole "templating stuff" though.
That might be underestimating the feature, it was very much the core proposition of the language and likely why it's so well suited for web applications.
They also share the same resources, so even your little blog can take as much (or more) traffic than your production application.
Then they tell learning CS theory is not required.
Naturally there are agreed names for other GC's, that is what widely acknowledged books in CS like "The Garbage Collection Handbook" [0], or papers like "A unified theory of garbage collection" [1] go about describing.
[0] - https://gchandbook.org/
Python uses RC, with GC available but only used for cycles.
Careful there :-) Modern javascript has semicolons and is introducing pretty horrendous syntax for private variables (an octothorpe) and possibly for the pipe operator (percent sign, was it?) and for tuples and records, yet is showing no signs of dying.
https://www.php.net/manual/en/features.gc.php
> This section explains the merits of the new Garbage Collection (also known as GC) mechanism that is part of PHP 5.3.
> First of all, the whole reason for implementing the garbage collection mechanism is to reduce memory usage by cleaning up circular-referenced variables as soon as the prerequisites are fulfilled. In PHP's implementation, this happens as soon as the root-buffer is full, or when the function gc_collect_cycles() is called. In the graph below, we display the memory usage of the script below, in both PHP 5.2 and PHP 5.3, excluding the base memory that PHP itself uses when starting up.
ARC is more performant but can introduce memory leaks via circular references, which is probably not a big problem for short-lived scripts anyway.
Perl uses ARC with no tracing GC, so you have to handle cycles yourself: https://www.perlmonks.org/?node_id=1173079
TCL uses ARC, and apparently doesn't need a tracing GC because it is impossible to create cycles in it: https://wiki.tcl-lang.org/page/Garbage+collection
Yes, PHP has reference counting, but it also has specific process that kicks in to clean up circular references.
Whole class of problems around multi-threading, mutexes, dead locks etc. are really foreign to PHP developers.
No. While PHP won't be the language of choice for academia or some new hipster startup that wants to woo junior engineers with the promise of "we're using the latest cool shit", the trio of PHP, Java and .NET with their associated ecosystems has managed the fine balance of evolution speed between uprooting everything on the regular (like NodeJS which went through at least three major build toolings with zero compatibility and the Javascript language itself with at least four ways of creating modules) and becoming a live fossil (like Perl) pretty well.
For PHP, just look at the Drupal, Joomla and Wordpress codebases to see how many ways there are to include files. For a current example, see the hate Laravel receives on how it does DI from Symfony-likers. There's also the Laminas (Zen) crowd going all-Java.
If something is popular, there WILL be many ways to do the same thing. Look at how Python is evolving.
Personally, in my career I've seen and worked with projects in SAP, Java (Tomcat), .NET, PHP and various JS-based projects - of all these, the JS projects had the most variety of bullshit (although SAP still gets the cake for "stuff I'll never willingly work with again" simply because how mind-boggling the entire environment is).
At least it's easy enough to get a bunch of Java developers to agree on what package manager and build tool they will be using - it will mostly be Maven and that's it.
For a nodejs project, you will first have to decide on the framework to choose, then on the build tool because not every framework works with every build tool, then you will lose a lot of time until Webpack finally does what you want, and in a year half the libraries you use will be unsupported.
Python and Perl use reference counting too. Ruby, Lua and JS (at least v8) are GC'd. PHP isn't unique in that regard.
And even if they do, what then? It’ll just be feature parity, so there’s no need to switch back either way.
Not nowadays, for sure. In 2009-2010 it was still something to keep in mind, and careful array handling was common so the it wouldn't baloon hundreds of megabytes for not so large data structures. I wasn't working on big data (tm) at that time, but running database imports via ETL processes in PHP required a few optimization passes.
I really can appreciate nowadays the huge memory and performance improvements that PHP went through from the 5.x days to the recent 8.1 release.
Sidenote: Shout outs to Nikita Popov who came to the scene and revitalized the language for me back then, for his proposals and features implemented in a time when PHP felt stagnant language wise https://www.npopov.com/aboutMe.html#accepted-php-proposals