PHP Is Finally Getting Generics...
Chapters10
Defines what an RFC is and who authored this PHP generics proposal.
Generics are nearing PHP reality via an RFC, offering real generics for classes and functions with runtime compatibility and static-analysis support.
Summary
nunomaduro explains that the PHP generics RFC is under discussion and could bring true generics to PHP, following the path of languages like TypeScript and Python. The RFC is authored by the creator of MAGO (Cified) and proposes bound generic types so that a function’s input type determines its output type. Currently PHP relies on annotations for generics-like behavior, but the new approach aims for real generics that don’t affect runtime behavior yet enable static analysis to validate types. PHPStan, Psalm, and MAGO would read these definitions for compile-time checks, while runtime PHP would behave as before. This means existing code could adopt generics without breaking, since definitions don’t carry to runtime. Frameworks like Laravel collections could implement generics for keys and values without breaking existing apps. However, the ecosystem-wide changes—IDEs, debuggers, linters, and static analysis tools—will take time to adapt. The discussion suggests a PHP 8.6 target, with broad community involvement and a two-thirds vote requirement to move forward. The excitement is tempered by the reality that 400+ files may need changes and that not everything may be approved in the near term, but the potential impact on PHP’s future is substantial.
Key Takeaways
- The RFC proposes bound generic types to PHP, enabling a function's input type T to determine its return type.
Who Is This For?
Essential viewing for PHP developers and PHP ecosystem maintainers who want to understand how generics could land in PHP 8.6 and what it would mean for frameworks like Laravel and tooling like PHPStorm and PHPStan.
Notable Quotes
"All right, maybe not that official, but it's under discussion. It's literally under discussion. We may actually get generics on PHP."
—Opening takeaway that the RFC is under active discussion and could bring generics to PHP.
"This is literally what this RFC is proposing."
—Clarifies the core proposal of the RFC—real generics with a specific syntax.
"The boundary means that the definition of generics won't actually carry to the runtime."
—Key distinction between static analysis and runtime behavior.
"Existing code will be able to implement generics without being a breaking change."
—Emphasizes backward compatibility during transition.
"PHP ecosystem moves forward together."
—High-level goal of ecosystem-wide adoption without breaking existing apps.
Questions This Video Answers
- How will PHP generics affect runtime vs. static analysis in practice?
- When could PHP 8.6 realistically include generics and what would break first?
- What changes would Laravel, Symfony, or PHPStorm need to adopt generics?
- Which tools (PHPStan, Magoo, Psalm) are ready to support bound generics before PHP itself, and what work remains?
- Can existing PHP annotations continue to be used, and how do they translate to the new syntax?
PHP Generics RFCBound Generic TypesPHP 8.6MagoCifiedLaravel CollectionPHPStanPsalmPHPStormStatic Analysis
Full Transcript
All right, maybe not that official, but it's under discussion. It's literally under discussion. We may actually get generics on PHP. On PHP P. I'm super excited about this. On this video, we are going to discuss everything on this RFC and what this can mean. Now, diving into this stuff. First of all, what is an RFC? RFC is basically a proposal to the PHP language itself. The author of his RFC is actually the creator of MAGO who have proposed bound generic types to the PHP language. Okay, what is generics to begin with? We kind of already have generics in PHP.
Okay, at the minute they work through PHP annotations, meaning that if I were to process this given value, PHP knows that the return type will be from the same value of the given argument. meaning that I'm not able to sum the given string nun maduro with a value one. So I get that information through PHP stand. Okay. So potentially here if I process one, PHP knows that the t value returned will be an integer. Therefore I will fix this issue. So we already have generics a little bit on PHP but they work through PHP annotations and people were not happy about that.
Now what this RFC proposes it's literally have real generics in PHP meaning that instead of having this crap right here you would be able to come here and say this function is generic and that generic type is called a t value and the gim type of value as an argument will be the same type of value returned. This is literally what this RFC is proposing. Now one question you may have is what means bound erased? Okay, I'm going to explain you exactly what this means. Boundary means that the definition of generics won't actually carry to the runtime.
Meaning that for PHP, and I repeat, for PHP, not static analysis, but for a PHP at runtime, this function will be actually something like this. Okay, will be exactly like this. Things won't actually change at runtime. However, for static analysis things like PHP stamp, solve or maggo, they will be actually able to read this definitions and perform static analysis on this code. This effectively means the following. If I were to go to this code right here and I were to for example return value with return no at runtime, this code would just work. It would compile.
It wouldn't actually tell you an error. However, if you use PHP stand, PHP stand will complain about this function because the return type actually depends of the given T value. I hope this was a little bit clear. And by the way, this is not something new. There is a lot of languages who have implemented generics this way. Python is one of them. Now, one of the many reasons I'm very very excited about this new RFC is that existing code will be able to implement generics without being a breaking change. That's very exciting. Meaning that classes like Laravel collection, they can effectively implement generics in this new approach without that being breaking.
Meaning that I can come here and say this will have a generic type T key but also a generic type T value. And by doing this this won't break existing applications because the definitions won't carry to runtime. However, for static analysis, things will continue to work like before because having this new syntax is effectively a little bit equivalent to the previous syntax through PHP annotations. Now this is super important to make sure the PHP ecosystem moves forward and moves forward together. Meaning that things like Laravel, Symfony and all the existing ecosystem will be able to implement generics in this new way without being breaking for every single application out there.
And of course, everyone is super excited about this. Just check this out. Literally, everyone is putting thumbs up. And I'm going to do the same right here. However, we're talking about 400 files changed. So obviously that is a lot of changes. Okay, there is a lot of changes on this RFC. So, this may actually take a while and actually may be up for a huge discussion. So, I'm going to keep yourself up to date on this one, but there is a potential of not actually being approved for this next PHP version. But everything is there.
However, man, I'm just so excited about this. Just look at this beauty. We're talking about PHP actually have generics. Beautiful generics, my friends, being implemented in the same way as Typescript, as Python. I'm very excited about this. Now, it's worth to mention that this RFC is not as simple as it seems. Meaning that if eventually this were to come to PHP 8.6, every single piece of the ecosystem needs to adapt to generics, meaning that IDs, things like PHP storm will need to support this new syntax. I I don't see a problem on that, but it may take a little bit.
Also static analysis we're talking about things like maggo will need to adapt PHP stand will need to adapt s and other static analysis will need to adapt but also many other things like debuggers coverage reflection so all that stuff will need to support generics and funny I just saw past php being mentioned on this RFC which is very very interesting but even things like llinters and everything will have to adjust to generics and that actually may take a little bit now it's worth to mention as tell that this RFC it's intentionally leave out of the scope things like arrays okay in itables as well.
I would love to see this RFC also have support for generics and things like arrays. However, I do understand that having that as well on this RFC may put the RFC in doing too much stuff. And we know historically that when an RFC have too much stuff typically doesn't get approved. So this RFC is literally generics just for classes and functions if I'm not mistaken. Now of course very important as usual just like any other RFC if eventually this were to move forward uh we will need to have two out of three majority uh accepting this RFC and this will obviously be targeting PHP 8.6 which will be released by the end of the year.
If you want to read more about this, I'm going to publish all the links to this RFC, the links to the GitHub pull request as well down on the comments below. Now, it's worth to mention that we are already getting a few things for PHP 8.6. However, in my opinion, nothing comes even close to the excitement around generics if we were to have them. Now, obviously, I'm very thankful for all the contributors to the PHP language and the creator of this RFC. It's called it Cified, the creator of Mago. So, thank you so much, Cified.
I apologize if I'm pronouncing your name incorrectly, but thank you so much for this. He is actually the creator of Maggo, which is a static analysis, a link formatter for PHP written in Rust. I have a full video on that topic right here in case you want to see what is Maggo. And write on the comments below what do you think about this new RFC. I want to see your comments about this new addition. If you want to see what we had on PHP8.5, don't forget check out this video. It's a beautiful video about everything we had on PHP.5.
Love you all and peace out.
More from nunomaduro
Get daily recaps from
nunomaduro
AI-powered summaries delivered to your inbox. Save hours every week while staying fully informed.


![[LIVE] TANNER LINSLEY: TanStack Start, React, AI Agents, and More thumbnail](https://rewiz.app/images?url=https://i.ytimg.com/vi/AQOPaHHYQFk/maxresdefault.jpg)


