Laravelvideo· May 6, 2026· 3:00
I'm going to let you in on a little secret. Modern PHP and the tooling that PHP has is actually really good. I'm talking about types, automatic refactor, static analysis, things that make code you write, code that your AI writes better. So, here's three examples of what it actually looks like to write modern PHP. Here's a class that I have. There's no strict types. AI models use up more tokens in this particular class because it's trying to understand what type of variable goes into this method. Let's compare it to this new class. I'm declaring strict types, which tells PHP
at runtime to throw an error if something is off. So, while things like static analysis can help catch things before runtime, which we'll talk about in a little bit, declaring strict types is enforcing that error to happen at runtime. Every parameter, every field, every variable has a type associated with it. So, AI knows how to work with it, your LSP knows how to work with it. My LSP and my Zed editor, for example, knows that this class of product has a string of name and an integer of price. Okay, next step. But, what happens if we
have this code, whether our AI wrote it or we did, that's just not right. It's just not standard. It's not proper. I have this paid variable that's declared, but it's not used. My editor is even throwing this hint to convert this to a switch statement instead of this multiple if return else statement. Not things that are wrong, maybe even things that AI wrote and said was good, but it's not something that you would want in your code base, right? So, fantastic tool is Rector, automatic refactoring
for PHP. And there's some documentation, some custom rules specifically for Laravel, too. I already have it installed. I have a configuration already set up for me. It's going to tell me what it would change to automate this code process. Declare strict types. Remove all of those private fields that we had. Adding return types. Simplifying if else statements. And allowing us to use modern features of the PHP language. Again, all these things aren't wrong in the first place, but it makes our files
that much more maintainable. Remove the dry run aspect and run it again, and it's going to make those changes for me. Lastly, everyone's talking about static analysis. So, what does PHP have? So, in this class of greeter, we have a public function of greet, and there's nothing really wrong with it. Tests pass, so we should be good, right? Well, I'll give you 2 seconds to see if there's an error. What if this user ID doesn't actually exist? If it returns null, what happens? So, this is where a tool like PHP Stan, especially with additions like Larastan,
specifically for Laravel, can come in handy. I already have it installed. I'm going to analyze that particular file, and we get an error. And while PHP Stan doesn't fix this directly, it's easy to now know what needs to be fixed. And so, we can fix this manually. We can have our AI assistant fix it. Our IDE is hinting us to convert this to string interpolation. We could fix this with just one go. So, maybe instead of this, it could be this. As another bonus that PHP has, there's this formatter called Pint that ships with Laravel. You just
run Pint or vendor/bin/pint. In this case, we fixed two style issues that were in those demos. So, yes, PHP can be modern. It can be enjoyable to write. And the more enjoyable it is to write for you, the better it becomes for you, for your AI, for anyone else who has to deal with your code.