YouTubevideo· Jul 7, 2025· 17:48· 29K views on YouTube

This is how you should write PHP

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

44 moments
  1. I've been building in Laravel and PHP for just over three years now and I'm still learning how to uh perfect my workflow in the sense that uh JavaScript, TypeScript has a ton of tooling in that world of how you want to maybe modify uh keep things up to date or even format things. And doing that in PHP is is a little different. Before we get back into the video, I want to give a shout out to the sponsor of the channel for this month. It's Bento. Now, Bento is an incredible service.

  2. To call it just a mail service, an email service is really an understatement because it's so much more. You can use it for transactional emails, for flow emails, like marketing emails, but it also has a incredible amount of other products of other options that you can use to really make your app great to make your product even better. And the founder and the support for Bento is absolutely topnotch.

  3. Uh Jesse, the founder of Bento, is someone who truly cares about making things, about creating things, about making a great product. He's been doing it for a while. And so, thank you so much for Bento for sponsoring this channel this month. Let's get back into it. But one of my good friends and co-workers, Nuno Maduro, released this video uh which is a talk from his Laracon India talk talking about modern PHP in 2025 and tools that you need to know. And this whole video, it's only 30 minutes. This is it's this talk from Lyric on India. It's fantastic. But I was there live. I got to listen to it.

  4. I got to see it. And frankly, I learned a lot. And so I put it into practice the things that I wanted to make my app and my in my case all my apps, my starter kits that starts off most of my applications. I wanted to make it better. And so I did just that. So I'm going to create a new Laravel application using my starter kit which is uh the dash using parameter josher/fvision and this does a couple of things behind the scenes but really I want to get into the tooling behind it.

  5. How am I using recctor php? How am I using pint or pint to lint to format my php files? And then how am I using prettier to do blade in my editors? and and and basically how do I format blade within my editors and how am I doing all of this within my specific editor which I'm using zed and I've been using for a couple of months.

  6. I still can open up VS code and nothing's going to change because I still have the extensions there and everything works how you would expect but I've modified this specifically to work within zed zed editor to work with PHP actor instead of PHP intellofphence.

  7. That's a interesting change but I'll show you why. And I did all of this mostly just so I could create a new starter kit, create a new layer of application using my starter kit, and then start building without having to worry about, okay, is this formatted the right way? Am I using uh the the proper PHP syntax? Am I missing any types and everything that kind of goes along with that? So, why don't we open this up?

  8. We'll open up the uh fision demo that we just created and we'll go ahead and go through this kind of piece by piece.

  9. Right out the gate, it says PHP stands detected and that's something that I'm using that I kind of got a hint from Nuno and something that I'm basically learning PHP better because of these modern tools. So, it's something that I want to implore you. How can you use something like PHP stand um PHP actor or PHP recctor I should say um as well as something like pint to be able to do all of these things behind the scenes and know how to think about okay am I doing things the proper way yes I want to

  10. enable the PHP stand extension it's indexing my workspace you can see here about 50% but we'll dive into the things that I learned from Nuno's talk again make sure you watch it it's fantastic But this just shows how I implemented that. How am I using the tools that PHP has today? It's not just a a language that you have to kind of like bear through and just figure things out as they break. There are so many tools that are available. And so the first thing I want to take a look at is Recctor.

  11. So Rector, if you don't know, basically allows you to say, okay, hey, here's this particular old way of doing something in PHP. And then it you can tell it I want to format that. I want to change it. I want to do this old way of doing something and then make it better, make it more modern. And most of the times it's it's new PHP features that allow you to do something that you didn't have the capability to do before.

  12. And so this is just a basic recctor config. Uh most of this stuff is stuff that Nuno had talked about. Basically saying, okay, any kind of dead code. and his whole talk goes through each one of these examples in detail. So, I'm going to leave that up to him so that way this video one isn't too long, but two, you get a little glimpse of what that talk is about because this shows, let's say, like an early return. You want to make sure you have early returns. You want to make sure you have strict bootlins. You want to make sure you have types.

  13. This is very much something like a biome would be within the JavaScript world saying I want my PHP code to look a certain way and recctor is going to help me format it to look that way or refactor it to make it look that way. So recctor is one piece of the puzzle. Next piece of the puzzle is going to be pint or paint and this is basically um just saying I want to format PHP in a certain way.

  14. Now, this comes in pre-installed with all Laravel applications where you can run uh the vendor bin pint command, but I have everything set up so I can run one command to do everything that I need, both run tests, refactor, and format. And so, that's all done within this. We'll do the composer.json. And so, I have this command here uh that is a test command.

  15. And the test command is going to test for typos. It's using uh peek php also created by Nuno. He's great at these tooling things. Um it's te testing all units. It's linting it. It's testing the types and then it's refactoring using using PHP recctor and then so mostly then the next step that I do is so test is basically checking all those things and then the next thing is fixing it.

  16. So, composer run fix which allows you to test the types refactor and then fix it using lintfix and pint. And really, those are the two commands that I I've I've kind of set up. Um, of course, we can lint it as well. But there there's a couple of other things that I've kind of set up just to have easier commands for most of my applications, but those two commands are going to be the two that I kind of look at. Testing everything. And this is going to not fix things. is it's going to not change thing.

  17. It's going to tell you tell me when something is is out of place or different. And then I'm going to have the fix. And usually most of the time I actually just run fix and then go from there. Uh but this works great to be able to say this is exactly the way I want my Laravel applications to work. The next step that I have is I have um in my package.json I use prettier.

  18. And so this is another kind of one of those commands that the composer dev is actually calling the mpm lint fix. And so that's using prettier to write and fix all of my not JavaScript files all of my blade files because I'm using this prettier plugin for blade. And so if we were to go into prettier right here, we're using the prettier plugin blade and the prettier plugin tail tailwind CSS. And we're formatting blade.php. PHP with blade.

  19. And so the cool thing is is if I have um a view and in this case I have this is a uh Laravel folio which is the pagebased structure as well as Laravel volt single file uh liveware components. So this uses uh I don't get the necessarily type hints and safety within PHP.

  20. Um well I do within my editor but I don't necessarily get let's say PHP recctor or PHP actor and all that or a recctor I would say uh within a PHP stand within this PHP block. But since I have zed set up to be able to format this based off PHP as well because it is

  21. set up to be PHP, we do have the ability of like these type hints of of saying like okay I want the user class and then I want um you know the create class out of that so that I can have them create specifically like there there's the type safe or type hints from PHP that we get because zed is reading this as a PHP file But then the neat part is if I wanted to change something down here and save it. You could see here that it formatted that, right? Uh let's mix things up here.

  22. I'm going to press command S to save. And bam, it saves it. Of course, it takes a little bit because it is running uh prettier behind the scene. So what's happening is it is running I can even open up our settings command. So what's happening right here is it is running the prettier path. So anytime we are saving PHP files um and it's going to throw a little error if we're saving PHP files that don't have the blade, but it's a small thing.

  23. Uh so anytime we're running PHP files, we're going to format them using the npx prettier command. And so that way we know that if this is a blade file that I want to format this with pretty even if I'm using livewire or vault for example.

  24. Next is PHP stand and this is one uh that basically this is the I would say llinter in a lot of ways uh and making sure that your PHP code is up to date in the sense that you want to make sure that you have a specific and this is using Larstan behind the scenes.

  25. Again, Nuno's talk is a great guideline, a great resource to say what all these do to great depths, but PHC stand makes sure where Recctor kind of formats that or reformats that and refactors it. PHP stand lets you know if it is actually, you know, proper PHP, does it have types? Does it have um like is it is it properly uh written? Is it static analysis? So, does it do the things that it says it's going to do?

  26. And so um that is using uh layer stand behind the scenes to have some great defaults out of the box. Um so that you can definitely check if you would like that.

  27. But with pint with peek which is spelling and with php stand and recctor I have some great things. And php stand allows me also to say I want to have strict types across all php files. So if a new php file is created let's go ahead and let's do that. So let's say um if we have app um and let's do maybe uh let's do models and and let's go ahead and create a new model.

  28. So we'll go PHP artisan make model and we'll create a new to-do model. All right. And in the to-do migration we want a table string and we'll call this a title. And then we'll just go ahead and do that. that and then the new model you can see here that we don't have strict types for this. So because that declare strict types is not set um this file itself does not need strict types.

  29. So what do we do? We can run we can run composer run test. What is this going to do? It's going to run all of those things and oh we get a typo event. So let's see. We can see here that create todos um is a is a typo. We can ignore that specifically or we can just bypass it al together.

  30. And I can go ahead and in my uh peek.json.

  31. I'm going to say let's go ahead and add a new word. And I'm going to have this be to-dos. Again, this is great to be able to say, did you make did you want to actually have that word? It's going to check all of my files, and that's one of the things that Peek does for me. So now, if I was to go ahead and run that same thing again, composer, run tests, no misspellings were found. Great. Um, and then look, it we have no misspellings found. Now, it's running actually the test. The tests work except we have style issues. Hm. Okay. So, we have final class declare types.

  32. There's things that are failing. And this is I believe this would be using a PHP stand to like run all of those tests, those static analysis tests. So we can fix it.

  33. You can see here that if I was to go into that to-do model, it's just a blank thing, but it doesn't have final class, doesn't have declare strict types, things that I've set that I want to make sure all of my PHP files have. So if I was to run composer run fix, all of a sudden it's going to do those things for me. It's going to make those changes. Recctor is going to run. And now all of a sudden in this uh file.

  34. Let's go ahead and see. Maybe I need to Yeah, it's cuz I had it open. So now we have declare strict types. We have a final class. All these things that are mostly just making sure that I don't shoot myself in the foot later on.

  35. Again, stuff like this modern PHP tooling is is helping me learn how to write PHP better. I didn't necessarily knew I needed these, but the few times that I have needed them have been extremely helpful in the sense that there's so many times when I'm writing code or maybe I'm having Claude write some code for me and it just doesn't work the way I thought it did and it's so hard to find maybe where is it failing.

  36. But these guidelines I would say even especially more for AI generated code. So if you are using AI generated code, stuff like this probably helps more than if you were writing it by hand. It helps to be able to say, do I have these guard rails, these guidelines around my application so that when I do write code, it is the way I want. And having these modern toolings to both find it and then fix it is extremely helpful.

  37. Again, with everything that we kind of talked about, it's helped me learn how to write better PHP code, but more appreciate the tooling that PHP provides to be able to say, do I have any misspellings in my app? Do I is it formatted the right way?

  38. And now I can use prettier with all my blade files, but then also pint within all my PHP files, but then is it actually written the way a proper PHP dev would write this? And let's say you didn't want to take my starter kit, copy everything out of it, or even watch Nuno's video and find all the little features and all the little configuration files and make sure that you do it exactly that way.

  39. Well, Nuno created this essentials, this Laravel Essentials, and this was actually the starter or the the backbone behind most of the changes that I made in my starter kit, which is just giving you better defaults for your Laravel application.

  40. strict models, eager loaded relationships, immutable dates. There's more than I even talked about. I really talked about the PHP tooling side of things like within test and fix to make sure those refactors are are up to standards. But this does even more. And I and this is kind of what started my path to make all of these changes to my starter kit to make all these changes to learning what PHP tooling can actually provide because there's so much that I think allows you to find these great

  41. starting points in order to build your application again to have those guard rails so that you don't have to worry about if something is going to break down the line. uh strict models, autoeager loading, unguarded models, force https, uh artisan commands like make action, which is something that I found very useful, being able to have those pint and recctor commands.

  42. And again, I changed some of this to be able to fit what I wanted to have all the uh pint as well as um you know, linting as well as spellchecking all in one command. But all this is a great starting point. And really, if I wasn't using my starter kit, I would install this into every application that I build because I think it gives those defaults.

  43. Again, maybe not for everyone, but these defaults are great to say, I have these guardrails in place so that AI or that myself cannot write bad code or when I do, I'm alerted to it. So again, if you don't use anything in my starter kit or if you even you don't watch the full uh video from Nuno's Laracon India talk, I would check out Laravel Essentials because it really is a great starting point. Now, back to the video.

  44. Most of us can do it for me or at least tell me when something is wrong. And that's really what it comes down to. Being able to have those guard rails around your application to make the best applications that you can so that you don't shoot yourself in the foot, so that you don't uh stumble along the way to allow you to keep building and to keep creating.

just you here