#15 New features for PHP 8, organizing data with Laravel Collections, and more
Welcome to the 15th Andy's Web Dev Tips newsletter! Here's a few things from the last couple weeks:
Try out all the new features in PHP 8
PHP 8’s official release date is just a month away, and with it comes some interesting (and maybe a little controversial) new features for the language. Marcel Pociot put together an interesting blog article that showcases these new features, while giving you the ability to try them out with live code right on the site!
I definitely think that my favorite addition is going to be union types. I already try to consciously type hint my method returns, but sometimes it can be hard when you need to return more than one type. Instead of abstracting that out into two different methods, this takes care of the problem completely.
Organizing data with Laravel Collections
About a week ago I released a new video on my YouTube channel, this time discussing my favorite part of the Laravel framework, Collections. It’s a little longer at around 30 minutes, but goes over what collections are, how to create them, and 12 different examples of using them with a real-life large dataset from Reddit.
If you don’t want to sit through the entire video, check out the description for helpful links to specific collections methods that I’ve bookmarked.
Casting JSON columns to value objects with Laravel
Jess Archer published a super helpful article on their blog about casting JSON columns to value objects in your Laravel app. What does that mean exactly? Well, take how date columns can be used with Carbon to format inline with something like
Jess goes through how to accomplish this on your own JSON columns using The Laravel Way, creating a custom cast and then assigning that to your model attribute. Additionally taking it a step further, Spatie’s Data Transfer Object package is used to validate the data and handle the casting itself.
Upgrading your server to Composer 2
Composer 2 is finally here, and with it comes a flurry of PHP developers eager to use it. And why wouldn’t you want to, since plenty of graphs and charts have been published showing a massive increase in download speed for packages like Laravel.
Laravel’s own blog published this handy short article showing how you can get started upgrading your server to use Composer 2. Spoiler alert: It’s pretty easy, and involves one line. The hardest part is ensuring that the packages associated with your application are compatible with the new version, but if they are then it’s smooth sailing!
Today I learned
This helpful hint comes from Liam Hammett on Twitter. When working with PHP use statements, you don’t have to include the entire class name. In some instances, that might actually be hurting your code’s readability. You can instead use partial class names, and even import multiple namespaces on one line!
That’s it for now! If you have any questions about the above, or have something you’d like me to check out, please feel free to let me know on Twitter.