DEV Community

Discussion on: PHP needs its own ES6

Collapse
 
aleksikauppila profile image
Aleksi Kauppila

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.

Collapse
 
dopitz profile image
Daniel O. • Edited

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: 3v4l.org/EKmWb

Collapse
 
aleksikauppila profile image
Aleksi Kauppila

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.

Collapse
 
johannestegner profile image
Johannes

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! :)

Collapse
 
aleksikauppila profile image
Aleksi Kauppila

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?

Thread Thread
 
johannestegner profile image
Johannes

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! :)

Collapse
 
hamatti profile image
Juha-Matti Santala • Edited

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

array(1,2,3,4,5,6)
  ->filter(function(num) { return num % 2 === 0; })
  ->map(function(num) { return num * 2 ;})

that would then transpile into

array_map(
   function(num) { return num * 2; },
   array_filter(
     [1,2,3,4,5,6],
     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.