Skip to content

PHP needs its own ES6

Juha-Matti Santala on February 07, 2019

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 soluti... [Read Full]
markdown guide

This is exactly what Facebook did with Hack. It also adds a decent type system, the vec, dict, and set primitives which replace all the ridiculous associative arrays, and gives you a high performance JIT to boot.


Generics would solve so much problems with arrays. They would allow us to create type safe collections where we could implement all those functions (map, filter, reduce etc).

The issue is that array is not an object in PHP when in Python and JS arrays and lists are object.


Sure. I took a quite literal ES6 way of thinking here in a way that it would require some compiling/transpiling back to original.

So maybe we could have

  ->filter(function(num) { return num % 2 === 0; })
  ->map(function(num) { return num * 2 ;})

that would then transpile into

   function(num) { return num * 2; },
     function(num) { return num % 2 === 0; }

so that it would be compatible with real deal.

Like I mentioned, I'm not a language designer nor do I know much about how to make languages or compilers but this is the high-level thinking I have.


At the least, generics would allow us to create our own array classes with generic strict types and I would bet that projects like symphony (which have a quite good robust collection implementation) would use this in the future. I am really hoping for generics in 7.4, as it is one of the most useful features in a "real" typed language! :)


Unfortunately AFAIK there's no signs yet of generics being implemented in PHP. There is an RFC for generics targeting PHP 7.1, but i don't know if it's still active. Maybe in PHP 8?

Aye, I have seen the RFC and I do hope that it is picked up before 8.x. Seeing how much that have changed in the 7.x versions (and especially seeing even harder "types" in 7.4) I do feel that it would not be impossible to see the RFC picked up in 7.4 or 7.5. I do hope so, but I'm not holding my breath! hehe

Either way, the additions in each new minor version of the 7.x releases have given me much joy! :)


Generics would be cool. On the other hand, it is already possible since PHP 7 to collect values of a single datatype. Just create a class and use type hinting. Here is an example:


This actually the approach i've been using for quite some time now. I usually just implement IteratorAggregate and create adding methods. Still, this usually means that i implement a lot of these classes. Lots of repetition, lots of new files.


This is very well put. As an outsider, my understanding of the last decade or so of PHP, there has been a lot of performance tuning but less done to ameliorate things about the interface?


Yeah, there's been a lot of talk and work on the performance and it's become way better with PHP7. From 5.4 on there has been small but super valuable improvements in the core syntax as well but since nothing breaks backwards compatibility and there's not much overlap with existing, the basics have not evolved as much as I'd like to see.


PHP introduced the possibility of strong typing with PHP 7 too (scalar and return typing) which is really cool I think. It's not perfect, but way better than PHP 5 or the ugliest versions before.


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.


I love the idea, and I think with the EcmaScript idea we could use a meta language. See all the new coming stuff like typed properties from PHP 7.4? What if we could use them now, and downgrade gracefully to PHP versions that do not support it by targeting a version like Typescript or Babel does for Javascript?


You might be interested in (@assertchris work, not mine :) )


Oh wow, that's really nice and seems to be quite in line with my thinking. A layer of new syntax on top of old functionality.

Could not help myself but start a research project. You will be able to transpile modern PHP code into valid PHP using build tools like Gulp.

This is so cool Khalyomede! I'll have to take a look and see if I could contribute also.


This is truly a gem! Thousands thank you!!! @Juha-Matti, if you plan on making an ES for PHP with this library, I am in too 😉


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 : - 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.


Great article about something I have been thinking for a while.

And its a thing that can done incrementally. (Ex: Array functions, File functions etc) without breaking any "old" code.

I like your array example. ;)



I have had so many headaches because of all the things related to arrays :D


Its too late for php in my opinion. All major companies have dumped PHP and moved on to node. Javascript is too big to fail now. Already ruling front end with React/vue/Angular, started to dominate backend and now with React Native/nativescript, it has native mobile app development covered too.


I don't think so. Sure, a lot of companies are moving to other platforms and have been doing so for years with ones like Rails or Django (and more recently, Node). But for example Wordpress and Laravel are still having quite an impact in the world on a maybe bit different use case and I think there's good work that can be done to help there.


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.


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.


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"


Have you tried Hack PHP from Facebook? It's basically what you're recommending here plus a lot more (strong typing and generics, for example).


I actually haven't. I've heard about it but never actually gave it a spin. Is it "backwards" compatible with PHP in a way that I could just start running my existing PHP app as a Hack app and slowly start using its better features?


Yes - I have had the pleasure of developing on Hack and I like to describe it as a blend of PHP, JavaScript, and Java, since it takes some of the best features from each (and adds more). It was built to be fully compatible with PHP so that Facebook could slowly migrate its giant code base without breaking any existing functionality.

That being said, I've never had to setup Hack on a server. The major difference is that Hack is a compiled language, so it requires significantly different server software to build & run (HHVM last time I checked). Depending on how your current PHP app is setup, this may turn out to be a simple or complex task.

Great, I'll have to put Hack on my list-to-learn and take a look at it in some point.

ES6 needs transpiling as well so needing to have a few extra steps with Hack is not really an issue.


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!


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, class shorthand, 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!


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.


the arrow operator looks really redundant. the closure'd be better "=>" (since "=>" is only used inside array key-value pairs and foreach)


Yeah, the thin arrow function is just something I threw in there when writing. I don't mind writing full function declarations, the main point was to get rid of the array_ functions


Imo, PHP needs to be euthanized, but as I say, just an opinion lol


The comparison is not fair.
You are comparing PHP, where a few functions are not consistent, versus JavaScript (or ECMAScript), where the whole language is a clusterfuck.

ps: Even his community is a mess. NPM... ha.


I disagree on both notions:

  1. I wouldn't say that in PHP it's only "a few functions not consistent". PHP has quite a lot of issues that stem from it not being built for this kind of use case originally but then being evolved on top of a wild wild west of weird standard library.

Like I mentioned in the beginning, it was a valid choice at the time for Rasmus to name the functions as he did.

  1. I personally think that there's a lot of similarities - not necessarily in the languages themselves but how they have evolved - in PHP and Javascript. Sure, 10 years ago Javascript was also a mess. These days, it's a joy to write with all the new stuff.

I've seen some recent JS tutorials where JS programmers make a pages/ directory and put about.js, contact.js in there and then do things like require '../utilities.js' for a dozen helper files - it makes me nostalgic for PHP circa-2005 ;-)


If you read it again, you'll discover it's not a comparison.


But you are saying that "es6" is the way to follow. Why?. Was the migration es6 painless?. of course not, it is painful and most developers aren't even using it (4 years and counting), they are using CoffeeScript, Babel or ActionScript, so es6 is a promise, not a reality.

So, do you think PHP need to start from zero or redone some stuff like the guys of JavaScript are doing?. Why?.

Javascript wasn't "redone from the ground up". They simply added a bunch of new/nice features. I would also have to completely disagree with "most developers aren't even using it". That is a completely untrue statement. You claim people are using CoffeeScript. Well, I'm sorry, but if you look at statistics CoffeeScript has had a sharp decline for a while now. ES6 is in fact the current standard of the the javascript language and it is in use. A LOT...

Your statements basically come off as an offense that somebody would say javascript is better than php. (Which the author didn't do, nor did he compare them.) The author is saying that the upgrade of the ecmascript standards was very good for the language. Then he said that an upgrade to the features of php would be good for the language.

I agree with Chad here.

I think PHP would benefit from a start from zero but that's never gonna happen. Too much will break if they do. So I started to think about alternatives.

I think it's great that in Javascript, not everyone has to use ES6. I see it as an enhancement rather than requirement and I think the "ES6-for-PHP" idea could follow that. A layer on top that people can use if they want but the code that gets actually run is still plain old PHP.

And I'm not saying that we have to take a Javascript route in PHP world. ES6 just happened to be a nice model that people who've done Javascript understand as a notion. It's not removing anything from Javascript, it's not forcing anyone to upgrade, but if you want to use it, it makes writing Javascript so much nicer.

And to reply your original question Jorge:

So, do you think PHP need to start from zero or redone some stuff like the guys of JavaScript are doing?. Why?

Yes. Not necessary "like the guys of Javascript are doing" but in some way. Do you not agree on the problems I've laid out in the blog post? With the wildly inconsistent naming and function parameters, wonky array functions and functionality and other problems?

code of conduct - report abuse