No Compromises

When you're building a Laravel app, you're writing PHP code, but you're dealing with many patterns and conveniences provided by Laravel. So where do you draw the line between the two? Is it important? We discuss this topic and how you can get the most out of what both PHP and Laravel have to offer.

Download our free eBook of Laravel tips: A Little Bit of Laravel
Fun game to learn/practice regular expression syntax: Regex Crossword

Creators & Guests

Host
Aaron Saray
Host
Joel Clermont

What is No Compromises?

Two seasoned salty programming veterans talk best practices based on years of working with Laravel SaaS teams.

Joel Clermont (00:00):
Welcome to No Compromises, a peek into the mind of two old web devs who have seen some things. This is Joel.

Aaron Saray (00:08):
And this is Aaron.

Joel Clermont (00:16):
When writing a web application there's a lot of layers we deal with and we work a lot with Laravel teams and people that identify as Laravel developers. A lot of times that's the focus, Laravel, that's sort of the abstraction layer we're working at when writing features. Now, obviously, Laravel isn't a programming language so it's built with PHP. So that's another abstraction layer, if you will, that we often think about. But I just thought maybe if we could today talk about the intersection of those two things and why it's beneficial even if most of the time we're thinking in Laravel terms. Where are the benefits that would come to us if we... Or, what would be the advantage of maybe fine-tuning some of our PHP skills and thinking just in general PHP terms even when we are building a Laravel application?

Aaron Saray (01:18):
Well, I mean there's some obvious answers. Like, well, if you take over an old PHP application and you want to convert it to Laravel, you kind of have to know PHP in all the underlying PHP a little bit better, because you're going to see things, and yeah, maybe you might click through some stuff. But the idea that you would discover everything by not reading the code or not understanding the core PHP is pretty silly, right? I mean, that's some of the more obvious. But I think that there's a whole layer and level, like you've been talking, of underlying PHP related functionality that can actually help Laravel applications be more beautiful and more put together nicely and have great architecture as well. And I come with that from two different points of view. First of all, Laravel itself is some abstraction around PHP, and if you look at underlying code there's a lot of good usage of various features in PHP and that may been bundled together to make it easier and faster for us to use them.
So instead of having to call 12 methods chained onto each other, there might be some sort of facade built-in to Laravel that allows you to do a couple things or configure it in the most common ways. So already the thing that you're using that you like, the authors of that have really delved in to understand how PHP works. But I think there's other levels too. There are some choices and some things that you get to make outside of the whole Laravel way of doing something or the whole ecosystem. I'll give you one example, because I think I'm being kind of abstract yet. For example, you have to combine maybe three or four static strings and then three or four different variables. There's a number of ways to do that and that's not really defined by Laravel, it's just a thing that happens in PHP. When combining a bunch of things it can get hard to read or hard to follow, so you could have single quoted text followed by a dot and then a variable and then a dot, to more text, and you can build together this chained long thing. You could use double quotes, and then put in variables inside of there depending on how it's put together, you might use... What are those little squigglies called?

Joel Clermont (03:42):
Braces.

Aaron Saray (03:42):
Squigly marks? Yeah, braces.

Joel Clermont (03:45):
I knew what you meant.

Aaron Saray (03:45):
I'm a professional programmer, I use squigglies.

Joel Clermont (03:49):
It's right next to the squiggly key on the keyboard.

Aaron Saray (03:51):
Yeah, you have to use that or whatever. Or, there's a number of other things too. Like, what if I wanted to pad a certain number? So I always wanted to have a bunch of digits so then when I combine that I have to do some more math logic, all that kind of stuff. Well, as you dig into PHP, you might find something called the sprintf, s printf, which is basically string prinf. It allows you to define a pattern that is combined with both standard words, letters, whatever you're trying to do, as well as symbols. Usually starting with percent, like %s is for string, %d for digit, et cetera. You can then define in there kind of like a template, almost think of it like a little mini blade file where you put together your very long string with all these little pattern markers in there. Then after that, you just put in all the variables in the same order as the pattern appeared and there you go. Then learning what sprintf is can actually help you make you higher quality, better looking, more easy to read code.

Joel Clermont (04:59):
Yeah. I mean, I would imagine, I don't know, I didn't look at this, but if you look in the docs for... How are we pronouncing it? Sprintf?

Aaron Saray (05:09):
Well, that's how I call it. I don't know. It's sprintf.

Joel Clermont (05:12):
This isn't a radically new feature only available in PHP 8. I mean, this is a core thing that's been in there since the beginning but it's also a really powerful tool. I mean, it's got a lot of different options you can pass and works different ways. But to your point, it does make it more readable and I like how you connected it. Since we're talking about the intersection of PHP and Laravel, you connected it to something we're familiar with like blade view templates. Like, those aren't scary, we know how those work and there's tokens and substitutions. So sprintF is the same thing. And I agree, that would be a nice example of how to make something more readable, maybe even easier to maintain without a whole lot of temporary variables and all these different things in between. One thing that I find with this discussion is, sometimes I guess there's this misconception and I maybe even has sparked an occasional Twitter argument, but this idea like, "Well, I do Laravel not PHP." It's like, "Well, they're the same thing."
I mean, Laravel is PHP, it's not a separate thing. But I think sometimes people that make those statements what they really mean is maybe there's not a clear line in their mind as to where does this functionality come from? Is this PHP code I'm writing? I mean, it's all PHP code but is it something I'd find in the PHP manual? Or, is this something like a helper that I would find in the Laravel manual? That's kind of where sometimes that line is a little blurry. If you're trying to figure out how to do something and the only place you look is the Laravel manual or documentation, you might be missing out on quite a lot of other helpful utilities that are just in PHP itself, so that's something I would keep in mind too to your point about sprintf. There's not going to be a page in the Laravel docs under string helpers about, �Have you considered sprintf?" It's something you'd sort of have to know from your own study of PHP.

Aaron Saray (07:15):
But I think there is some overlap too in some of those areas where I think about validation. One of the validation rules is RegEx, and it says, "Or, you can say RegEx and follow it by this." I mean, it's leading you down the path of, "Oh, PHP has the availability to process RegEx but it doesn't go on much beyond that." So then you have to open up the PHP manual and look at how RegEx works, then the various different matching tools and all that kind of stuff. Which is pretty interesting when you think about it because you talked about layers but then talking about Laravel has a layer on top of PHP, which PHP then has a layer on top of the RegEx processing engine.

Joel Clermont (07:57):
Sure, right.

Aaron Saray (07:57):
RegEx is not a PhP thing, PHP happens to understand that. But along those same lines, even as simple as understanding how RegEx work or whatever, that's one more that I think a lot of people find scary. But it's just like everything else where you just spend a little time or dedicate yourself a day and say, "Today I'm going to just learn about RegEx. I'm going to read a bunch and I'm not going to understand half of it and that's okay." So when you bump in all those things again, your brain will have that context of it and be able to apply it.

Joel Clermont (08:32):
Yeah, that idea about taking a day and learning it, I'll put a link to this in our show notes for this episode. But I found it really cool, almost like a game. I think it was in the form of a crossword puzzle or something that gradually taught you different regular expression syntax. Because it's one of those things, if you don't use it regularly... It looks like a cat walked across your keyboard, these backslash and symbols and things. But if you just pause, even knowing just 10% of what regular expressions can do I think will get you a long way. Like, you mentioned invalidation rules or if you're using a routing or other different places. So, yeah, definitely a good tip.

Aaron Saray (09:12):
Another one of these different levels and layers and things is, I think as a developer you're responsible for understanding which choice is maybe the best choice or which tool set is the best tool set to do something. There's nothing wrong with saying, "I'm going to do everything in Laravel and only with Laravel functions." If that's the most efficient way. Sometimes you might have to go out of your way to use those tools though when maybe there's something else built-in to the underlying language that can kind of help you out. Then again, a lot of those things are combined too. One example I'll give is the Standard PHP Library, or SPL that has a lot of different functionality built into it that goes a little bit beyond PHP, it augments it. You have the iterator, and you have the recursive iterator, and you have the array object and you have all these different things. You could say, "Yeah, I can use collections for that," and that's cool. Sometimes you're going to use collections for things, and collections underneath that maybe actually implementing some of these things.
Another thing I think about is the directory iterator and the recursive directory iterator, and being able to filter that sort of way. Yeah, we do have the file system functionality but what if you're looking at a local file system and you have to do some really complex delving down deep into multiple different directories and stuff like that? It may make more sense to actually use one of these tools that was specifically built just for that one sort of thing. It's not the abstract, so if you're using the storage mechanism or whatever, it's an abstracted way and it handles everything kind of the same. But in this one case, if you understood your use case it might be that this other tooling in the other layer is more effective or more efficient, or one of those things.

Joel Clermont (10:58):
Yeah. You mentioned that sometimes even the Laravel conveniences will kind of under the hood use some of these things and I've definitely found that to be true. Like with the iterator is, while you were talking I pulled up the php.net page on SPL and there are way more iterators than I remember there being. Very specific use cases of ways that you could iterate over a data structure. So not even knowing what those options are, you might be missing out on a solution to a problem that, yeah, maybe you could solve it another way manually. But knowing that, for example, the no rewind iterator exists. I don't know why you'd use that I'm sure there's an awesome reason to. But that's one of those things that you might have to go poking around the docks a little bit just to see what's out there, to have it in mind when you come across a problem where that might be the right tool to use to solve the problem at hand.

Aaron Saray (11:54):
Yeah. I think the thing is when we say, "We're Laravel developers," we do mean we're programming PHP but we're utilizing Laravel to save time, to organize, to do these different things. And I think that's going full circle even to some of those things you mentioned on Twitter. When people say they're a Laravel... "I'm a Laravel developer, why would I need to know PHP?" What maybe the conversation should be is, "I stretch and I use Laravel to the best of its abilities to save me time. So I focus maybe on there first before I think about PHP, or vice versa." But as we've demonstrated, there's reasons to dig in past just the Laravel layer.

Joel Clermont (12:34):
Yeah, for sure. The other argument I hear which I don't agree with is, "Oh, that person only knows Laravel, they don't know PHP." Well, that's impossible, right? You can't possibly be a Laravel developer without knowing PHP. So I think it's a great entry point to the language, it gets you going, it gets you productive. And honestly there's things I've learned about PHP just by looking at Laravel source coder documentation, so it's a two-way street. The two benefit each other so, yeah, I think it's good to know both for sure.

Aaron Saray (13:08):
I thought I was wrapping up the episode, but apparently Joel wants to have the last word.

Joel Clermont (13:13):
No, he doesn't.
I want to ask you, Aaron, if there's something you can relate to from your school age years? Something I was discussing with my family last night and both myself, my wife, my adult son, they all could relate to this. It's this peculiar form of, I guess for me and maybe most people, embarrassment at having to try to do a pull-up in front of a group of people. I'll paint a picture. Its gym class, maybe its middle school, maybe its high school. Normally, you're doing sports related things. You're running, you're playing baseball-

Aaron Saray (14:02):
Volleyball.

Joel Clermont (14:03):
Right, exactly. But then one day randomly we're going to line up and we're going to see if you can do a pull-up and how many you can do and everybody's going to watch you try to do that. Maybe you have to climb a rope, maybe you have to jump rope, just these random skills that you're being tested at. I'll be honest, I could not do pull-ups. I don't think most of the people in my class could do pull-ups and we certainly never had a unit in gym class training us how to do pull-ups. Maybe exercises you could do or muscles you could strengthen or techniques. No, it's just like, "Show up, how many can you do? Oh, zero. Again, Joel, zero." Can you relate to any of this, Aaron?

Aaron Saray (14:48):
Yeah, don't get me [beep 00:14:49] started, okay? Here's the deal. I've always been a little bit of a round child. When I was younger, my parents decided that they would call me Buddha. That was great and then you can just pretty much assume that imagine a baby roundness just never going away. So I was never really good. I mean, I play some volleyball, that's why I remember. Then, yeah, like you said just one day they're like, "Oh, cool." You're already feeling bad about your life because you're a teenager now and everything is all weird and whatever. Then there's this really strong kids that are in gym class just showing off. And by the way, that kid now he pumps gas. I know it's a joke, but it really happened. That one kid, he could climb the rope great. Now he just pumps gas at my local... Basically where I grew up.
Well, anyway, they're like, "Oh, let's all line up." Everyone's like, "Oh, well I did one or two." Even girls are doing one or two. Then I go there and I joke, "Can I do the bent arm hang?" And they're like, "Ha-ha, no." I'm like, "I can't do one." They're like, "Well try." I'm like, "I don't need to try. I know I can't." They're like, "Well, just give it a try." You go up there and then everyone's looking and you're just like, "I guess I'll just hang here like a teabag. I just don't know what you want from me." So I hang just a really fat juicy grape and kind of just sit there. Then they're like, "Oh, I guess, aren't you trying?" "Yeah, I'm trying." Or then they said, "Get on the rope." And they're like, "Oh, climb up the rope." I'm holding onto the rope, I got my legs clamped up. They're like, "Why don't you pull up?" I'm like, "I am pulling up. I guarantee you I'm pulling as hard as I can. The fact that I'm not moving doesn't mean that I'm not pulling." Oh, it drives me nuts. And I just want to thank our presidents for that, because if-

Joel Clermont (16:27):
I want a name. Which president put this in?

Aaron Saray (16:30):
We'll have to look. But if you didn't know it's something, especially maybe for our listeners across the world too. There is something called the President's Fitness Test and that's what this was. It was like, "Can you do like 50 setups in a minute, and can you do..." I'm like, "No." This was back in the 19... Where people were more fit and I still couldn't do it. Thank you for bringing that up, Joel.

Joel Clermont (16:59):
I feel like I touched a nerve here.

Aaron Saray (17:01):
Oh, it�s really hot here now.

Joel Clermont (17:03):
Sorry. It's all right, Aaron. There are no pull-ups on this podcast.

Aaron Saray (17:07):
Yeah. Well, as I got older I became a little bit more healthy. I've been to a personal trainer and everything, still can't do pull-up.
Earlier today Joel and I were self-googling our podcast and found we're really not in the top rankings of Google.

Joel Clermont (17:25):
Can you help? Share our episodes around with your fellow devs, give us a rating in your podcast player or subscribe so you don't miss an episode. And that way next time we do a Google search, we won't feel quite so bad.