🧵 A lot of people have been asking for more type-hints in Laravel. In this thread I hope to clarify why this probably won't happen (anytime soon). #Laravel

Disclaimer: everything in the tweets below are personal opinions and doesn't reflects Laravel as a whole. https://twitter.com/driesvints/status/1379345997034352640
I want to talk about some decisions that make Laravel the framework in the way it's built right now. Wether you love it or hate it, Laravel is very open to extension. Its classes are extendable, most of its methods are public or protected and there's almost no scalar type-hints.
This brings some big advantages to users. People can manipulate the framework (or abuse it like some people like to say) in ways to make it work how they want it to work. Instead of policing how things should work we give more power to our users.
This also brings disadvantages. People will manipulate things in ways they aren't intended. One could argue that people are adults who know what they're doing but there'll always be cases where you can't protect people from themselves.
Which brings me to type-hints. As most of you know I'm a big fan of type-hints. They allow for api clarity, prevent runtime errors early on and simple make code more clear (personal opinion).

Now why is it that Laravel probably won't adopt them soon?
First of all you have to be aware that most changes to a method signature are breaking changes. Adding type-hints or return types will cause code overwriting those methods to fail hard. We're always very careful not accepting these changes on stable releases.
If Laravel would be adopting type-hints and return types then it would mean that these changes were to be the single biggest breaking changes the framework will ever introduce. The upgrade path would be hard and very steep.
Because of what I said earlier about extensibility I believe tons of code across the ecosystem will need to change. First and third party packages would all probably need to release new major versions. Apps would need much more time to upgrade than with other new major releases.
It's true that maybe tools like @laravelshift & @rectorphp can help here. But in general the cost of upgrading will be severe and it'll probably take long before the entire ecosystem has caught up.
The changes that @phpunit introduced with the void return type on setUp and tearDown are still keeping some libraries from upgrading.

Introducing type-hints & return types in Laravel will be *much* more severe and will probably keep apps/packages on old versions for a long time.
Besides the impact it'll have as a breaking change you also have to consider what it'll mean for the nature that is Laravel. I spoke about extensibility earlier. With type-hints and return types Laravel will fundamentally change a bit in the way it works.
The question we have to ask ourselves is if this is wanted. There's grace in the way how Laravel currently works with being open and extendable. Do we want to tighten it? Make it less flexible?
I personally believe a typed codebase is a good thing but does this also goes for a framework that impacts thousands of apps? Is it really worth the cost? I believe we should really think about this long and deeply before we make that choice.
This brings me to the "when" part of this thread. If they're going to be, when will type-hints be introduced? At the moment Laravel v9 will have PHP 7.4 as its minimum version because 7.4 is only EoL at the end of 2022. There's a big reason to wait: Union Types in PHP 8.
There's tons of places in the framework where we make use of union types. To make use of native union types PHP 8 needs to be the minimum version of the framework. This will happen in September 2022 when Laravel v10 is out.
Why not introduce type-hints and return types in Laravel v9 and then union types in Laravel v10? Because of the severe breaking changes I mentioned earlier. It's best to wait until Laravel v10 and swallow the bitter pill just once instead of twice. Because bitter it'll be.
A lot also depends on what Symfony v6 is going to do. I'm not sure what the exact plans are but I suspect that Symfony v6 will introduce a lot more type hints. Symfony v6 will probably become the new minimum version in Laravel v10. Another reason to wait until then.
I hope this sheds some light on the consequences and timing are for type-hints in Laravel. At the moment there's no guarantee that we'll introduce them. Its change will be one that'll change Laravel's nature for sure. And we have to decide if that's the path we want to take.
I want to close of by saying that on a personal note I believe type-hints are the way forward. Making use of these native language features brings so many benefits. But I'm just one person and don't want to impose my choice onto others.

Time will tell what'll happen.
You can follow @driesvints.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: