PHP generics aren't coming…

preview_player
Показать описание
I gave a talk about the current state of generics in PHP.

In this talk, I'll cover the basics of PHP's runtime type system, explain the concept of static analysis, explain what generics are, look at the reasons why they are impossible to implement in PHP, and talk about possible solutions.
Рекомендации по теме
Комментарии
Автор

- What we do need is a standard definition for doc-blocks, so that all IDEs and static analyzers follow it and that's it.
- Not everyone should be bothered with writing generics, this is good for frameworks and libraries which are downloaded andd used by others.

IMANGHAFOORI
Автор

You're answering all the questions that I have popping up while I'm watching your explanation, so... Brent. Another very well thought out video. 👏

To be honest, I'd love option #3 - In a way, the mind-shift we'd need, we're already there. We can already use PHPDocs for this, though:

Maybe a overview of how we already can use PHPDocs for these, would be a great follow-up? If we need the mind-shift, we'd need the clarity that this is static-only, just as Python?

Xewl
Автор

The Generic types in PHP are possible, I have already written an implementation of generics in a PHP framework called DevNet-Framework, using PHP Attributes within the generic class that uses a TypeTrait which is responsible for the type checking when calling a method that has a generic type parameter, this is a kinda hacking solution, but this idea or similar thing can be integrated into the PHP Core without playing around with attributes and traits.

maometos
Автор

Thanks for another great video!
I think that attributes could be a good compromise to extend the PHP type system and I would love to see some extensions for PHP IDEs and static analyzers that would allow us to interpret those, so that we could more easily come up with new additions potentially turning into common best-practices by popularity rather than trying to come up with a standard that serves all purposes up-front.

I recently created a little library that allows me to do this at runtime (apparently I can't share the github link to it here) it is a bit quirky and has a performance impact (due to the runtime checks via reflection) but it turned out to be very useful for me

bwaidelich
Автор

The third option made me think about atributes/annotations... couldn't we achieve something similar with those?

brunoggdev
Автор

Good summary of all the blogposts you've written on this subject

jelmerprins
Автор

I was waiting for the type erasure part 😂😂

yungifez
Автор

Such a great talk. I know generics in PHP was very high on my wishlist for a while now. And yep, one of the biggest reasons why is because it’d be so useful for collections/lists. That said I can totally understand why that runtime type check would be a big hurdle. I’m with you on type erasure too; but I _also_ immediately started to wonder about the inconsistency between generics (erased, static analysis only) and all other types (runtime)… maybe TypePHP which compiles to PHP? 😅 ok lol j/k, now I’m trolling.

patricknelson
Автор

Why do you think we can't just decide, per config option, if PHP should check types on runtime or not?
If I am relying on static analysers, then I would make PHP simply ignore types checking.

Safenabo
Автор

I think generics via PHP attributes like #[<T>] is a way to go.

Asgraf
Автор

I guess even monomorphic generics in PHP are possible as well. PHP already has the concept of opcache preloading. Nothing prevents adding such a thing as an opt-in feature but for achieving monomorphic generics. By default, generics could be erased in the runtime, but when enabled **generics preloading** the engine could make a direct reference to the correct class.

But I want to add that I am not a core contributor and cannot implement it.

taras.chornyi
Автор

Hot take incoming: I believe that php is too widely used for the argument "the implemention would be too complex". It shouldn't be considered impossible because refactoring is hard. Just take the time. Generics are way more impactful than the most other updates for the language and I'm willing to wait for 2 years to get them over any other feature.

xtay
Автор

The @template annotation is generally accepted, but when it comes to types, everything is interconnected. Library authors would potentially need to figure out typing for every possible static analysis system.

ShadoFXPerino
Автор

Same as how JS may potentially absorb TypeScript in the same manner then; syntax without doing anything.

mattwilcoxuk
Автор

PHP has interfaces and traits, is loosely typed and does not require compiling .. one could argue that generics are not really necessary

mjdev-ip
Автор

It sounds like PHP needs something like TypeScript which will allow us to right code with generics, handle the type checks and compile down our code to without generics version to deploy.

bulent
Автор

Thanks for the video. Gives a good insight in what is involved in implementing them in this language. I hope, someday, Generics will be part of the language nativly so we don't have to add comments all over the place. Or if it wont, the community will agree upon a certain standard for comments to implement generics.

jsomhorst
Автор

I know you are working for JetBrains but I would love to see a thorough guide about static analyzers

Darkray
Автор

Thanks again for this video. While Php has added so many types throughout 7 to 8 and while I was working with JavaScripts superset TypeScript real typing feels quite uncomfortable in PHP. Even when using Static Analyzers i feel like i have to write the same types all over throughout several PHPDocs. A mature superset transpiler like TypeScript for PHP would leverage the php community so much. The godly 'array' type also has much potential to be improved. Obviously it will be very hard because modifying array would be breaking. But how about a new data structure

Darkray
Автор

Why can't we have generics that are just syntactic sugar that don't have to do anything during runtime.

zbynekriha