I have a love-hate relationship with PHP. I have written PHP in many forms from website templating and Wordpress to full MVC and SPA backend solutions in the past 15+ years.
I was reading through Bronson Dunbar's post "Using and learning ReactJS for 2 years, what have I learnt, and I stopped at this:
As we mentioned earlier, JavaScript has been around for some time, and in order to stay relevant a few people decided it was time to give it an update and that is when ES6 was born.
Both Javascript and PHP have similarities in their journey. Neither one was built for what they are used now: Brendan Eich wrote the Javascript as a prototype in 10 days back in 1995 to provide Netscape interaction into browser and Rasmus Lerdorf wrote PHP to be a templating engine in 1994. Due to the popularity of both, they have evolved into something completely different.
For the past few years (after getting over the Python 2->3 pain), I've been thinking and speaking about how I want PHP to break backwards compatibility. I know it's not gonna happen because such a big part of Internet runs on PHP and it would break everything.
So Bronson's post gave me something to think about: maybe we don't need a "new PHP", maybe we need ES6-for-PHP — a layer on top of PHP that would allow us to tackle the issues and write different PHP while still being compatible under the hood.
Background
I'm no language designer nor someone who finds joy (nor has skills) in building new programming languages. But I'm a dreamer and I can dream.
One of the big annoyances in PHP is the inconsistent standard library. Which is actually a feature, not a bug. When Rasmus Lerdorf was creating the language, he used different kind of naming schemes to balance the function hashing.
Well, there were other factors in play there. htmlspecialchars was a very early function. Back when PHP had less than 100 functions and the function hashing mechanism was strlen(). In order to get a nice hash distribution of function names across the various function name lengths names were picked specifically to make them fit into a specific length bucket. This was circa late 1994 when PHP was a tool just for my own personal use and I wasn't too worried about not being able to remember the few function names.
But it's 2019 and a lot of PHP is still being written. What if we could make it more enjoyable? (I love writing Ruby and Ruby on Rails and DHH's The Rails Doctrine is an inspiration to me. Especially the part about developer happiness.)
So what should we work on?
Consistency-layer on standard library naming
As you can see from the quote above, PHP's functions were named for specific purpose: to balance the hashing function. It means that as the standard library has grown, it's impossible to remember how to write function names because there's no consistency.
There's strpos but str_rot13. There's php_uname but phpversion. There's strtolower but bin2hex. And there's str_shuffle but recode_string. You can probably see the point.
So first plan of action: creating a consistent and predictable naming scheme
Turning array functions into methods of arrays
Let's take a look. Let's say we have an array of values that we want to first filter and then map. In vanilla PHP, we would do this:
array_map(
function(number) {
return number * 2;
},
array_filter(
[1,2,3,4,5,6,7,8,9,10],
function(number) {
return number % 2 == 0;
})
);
Notice how array_map has parameters as callback, array and array_filter has parameters array, callback. I have no clue why they are the exact opposite of each other but more often than not, I don't remember which is which and I have to resort back to docs. Also it's difficult to follow because of heavy nesting.
Let's see how we could make it nicer.
array(1,2,3,4,5,6,7,8,9,10)
->filter(num -> num % 2 == 0)
->map(num -> num * 2)
Making array functions into methods of array itself, we could chain things. Even if we don't want to adopt ES6-style arrow functions for anonymous functions, it would make this code much easier to follow and ready.
Second plan of action: make array_ functions into methods of arrays and make then chainable
One sort to rule them all
How about sorting then? Currently PHP's sort is a huge mess. Back in 2015, I wrote a blog post about my pain with them. Quoting myself:
There’s of course sort. Instead of giving a flag or parameter to
sortto reverse the order, you haversort. Then if you want to keep the key=>value pairs intact, there’sasort(which has caused me most confusions ever since I accidentally used that one instead of sort) andarsort. Then you can sort with keysksortand reversekrsort. For natural sorting there’snatsortand case insensitivenatcasesort. And when all this is not enough, you can use custom defined comparing function withusort,uasortanduksort. And the inconsistently namedarray_multisortfor multidimensional arrays.
What if instead, we would have just sort() function and that would work with either flags, keys or custom callbacks. And please have an option for sort that returns array, not just sort as a side-effect. One of the first custom functions I create in most PHP projects is a sorted function (name borrowed from Python) that enables me to be more functional.
Third plan of action: unify sorts
Separating sequential array and associative array
Did you know that PHP only has associative arrays? It works quite okay while you're in PHP land but when you start converting it to JSON, you start to see issues. Another one of my blog posts from last year highlights this issue.
If you sort an array with numeric, sequential and unordered keys, you turn from object to an array. If you sort an array with numeric, sequential and ordered keys, it remains an array. Whatever you do for non-numeric or non-sequential array, it will stay an object.
When I'm reading through code or writing it, I should be able to figure out more consistently what the outcome will be. Using array_values to reset a "once a sequential array that got turned into associative array" is horrible.
Fourth plan of action: separate array types
Conclusion
There are probably other parts of the standard library that could benefit from a "ES6 Treatment" but the biggest pain points of my life regarding developing with PHP are these.
Let's recap:
- Consistent naming
- Array functions into chain-able methods of array
- One sort, no more
- Two arrays are better than one
Which parts of PHP would you like to see enhanced with ES6-for-PHP type of solution?
EDIT Feb 9th
If you like the idea, check out php-next project by Khalyomede
Latest comments (54)
I love that
Whilst I have my own list of changes that would improve PHP, and I heartily agree over how derpy PHP's naming conventions are, some of what you said doesn't feel like improvements to me.
The daisy chained "building" methdodology of ->something->somethingElse always strikes me as painfully and agonizingly cryptic, and for what? "Wah wah, eye dunz wunna type?". PHP thanks to its C syntax is already painfully and agonizingly cryptic enough without making it HARDER to understand. One of the many reasons I'm not a fan of jQuery and often call it mentally enfeebled half-witted trash.
Things I'd like to see?
No more shorttags, EVERYTHING is code and nothing is allowed to blindly output.
No more heredoc, nowdoc, or double quoted strings. Single quotes or golf tango foxtrot oscar.
Make all "include" be require. The myth of overhead difference is BS and it would cause a fundamental shift in how people write PHP.
No passing scope to includes. BREAK SCOPE.
No more direct execution inside includes, make everything in an include HAVE to be an object or function method like any other normal library.
Make includes filetype be a trigger to NOT allow the file to be opened for read/write from PHP itself. That means no fopen, no readfile, nothing OTHER than including it for execution.
These might all sound odd, but do you realize how many PHP cracks/exploits would have fallen flat on their faces with these changes?
But what do I know? My first step after RCA 1802 and 8080/Z80 Machine Language was Pascal, and I spent a decade programming ADA. "C is not my favorite language"
Interesting. I call one approach I'm taking Shoop. Makes the base data types objects and wraps enough of the SL to perform interesting chains of transformations. I've enjoyed using it in the 8fold platform enough that I went ahead and made it public.
I do think Rasmus is participating in the direction more and we'll see PHP evolve in interesting ways moving forward. When PHP7 came out he gave a lot of talks about the history and his participation and what's happened to the language and core maintainers over the years.
I think the presentation by Christopher Pitt on Laracon Amsterdam 2017 would be interesting to you:
Transforming PHP
I myself feel the same pain and I'm investing time in PoC's within PeachPie the .NET compiler for PHP.
Yes, that's another compiler, not a new standard. But it provides me all the power of .NET, including semantics and syntax not (yet) available to PHP. Like generics and aspect oriented programming (AOP) with "attributes".
This is exactly why I don't want to continue on PHP (alone) because the evolution of the language is slow.
Unfortunately this is exactly the showcase of open source vs. proprietary software. JAVA has existed since 1995, as have Python. JAVA was backed by Sun and later by Oracle. Python, Perl and PHP have not been backed by companies or any industry.
The .NET framework has been backed/invented by Microsoft and was solely working with the Windows platform. Very early, almost immediately Miguel de Icaza started on Mono, a .NET framework outside of Windows.
Nowadays, this Mono has been integrated into the .NET Core framework, available for Linux and OS X. It is mature and very stable.
What this means? That both JAVA and .NET are frameworks, moving into platforms. Where are Python, Perl and PHP? They are still just languages.
These are fine for day-to-day little projects, but for future proof initiatives many of the languages don't have the oomph to grow into the same direction.
These open source projects need a clear vision, tight conventions and a rigid schedule to implement new features. Otherwise they will not keep up. They will become extinct because of natural selection.
Great article. I think the main problem with it all is that PHP is so widely used that core maintainers have a very strict policy of not breaking the api. But I think PHP 8 will deal with more api changes.
As a side note, I don't know how many BC changes ES6 brought to JS, but I'm inclined to think that they were new features mainly.
In any case, you should check Nikita's Scalar Objects extension. I've tried it before and has been great pleasure to work with. It really empowers you a lot, because it let's you define your own api over the native types.
Yeah, maintaining backwards compatibility is the key. And it's very crucial stuff because such a huge part of the Internet runs on PHP.
ES6 was an extension but for me, the things that made me fall in love with it (after a quite a while of opposition) were the changes into how Javascript is written.
() => {}shorthand for functions,classshorthand, template literals, etc.Thanks to this article, I've learned of a few ways to write more enjoyable PHP so I'm pretty happy about that. Scalar Objects extension looks pretty neat, thanks for the tip!
Problems with arrays mentioned in this post are the exact reason why I'm converting data to Laravel Collections whenever the chance comes. Collections are very pleasant to work with (method chaining for the win!) and I would love to see them integrated to the language itself. That and arrow functions, of course.
Following this train of thought, I'd fancy to be able to develop efficient, maintainable, and secure PHP apps without adding framework as a dependency but that might be too wild to dream about.
It's possible. I was developing with a framework based on Sylex (which is now based on Symfony 4). It's very light, you don't have to couple everything and it only use the DBAL of doctrine instead of the ugly ORM. There are other lightweight PHP framework out there, too.
That's really well put. Frameworks always come with quite a lot of extra weight. Especially when it's often the best parts of multiple frameworks that you want to take and that gets messy a lot.
Haxe has been supporting php for quite some time.
I had never heard of Haxe before, I have to take a look over the weekend!
Are you aware of phpsadness.com ?
Yeah, I've read through bunch of those sites. I read through some of them for memory refreshment when writing this article.
This one is another good one, although bunch of the stuff listed there have been fixed since.
I was going to mention the phpprocessor but I just noticed someone else already has :-) It's quite cool - but I've never been brave enough to use it on a production project even though I'm totally fine with webpack/babel/modernizr etc. :: shrugs ::
There was a PHP RFC a while ago about standardising the names in the stdlib : wiki.php.net/rfc/consistent_functi... - don't think it ever went anywhere though. The list of functions to be renamed is quite 'impressive' ;-)
Thanks for sharing the RFC! That list in it is both impressive but also scary to see in one viewing how inconsistent it currently is.
RFC it!