در حال بارگذاری ...

Addressing PHP Reputation0:00

[Music] Laravel can't be good because it's PHP. Before we can say anything about Laravel, we have to address the elephant in the room, and that is Laravel is PHP. And you know, I get it, because PHP earned its reputation. But the reputation that most people think of, or at least most people that haven't been exposed to modern PHP is the PHP from, well, a very long time ago. But, you know, that reputation was very

to modern PHP is the PHP from, well, a very long time ago. But, you know, that reputation was very much earned. It used to be wildly inconsistent. It was loosely designed, and it encouraged some, well, some very bad practices. And we can look at some very bad PHP. I mean, here we can see that, you know, these includes. And that is something that we had to do. And it wasn 't just in one file, it was in all of our files. We need to include every file that we needed to,

file, it was in all of our files. We need to include every file that we needed to, you know, include. So we would have a ton of includes or require wantses or things like that. Then we would have registered globals set to on. Sounds like a bad idea. And really it is. And if you're not familiar with this, at the time, it may perfect sense, because it took anything coming in from the request, be it a query parameter, or a form field, it turned that

anything coming in from the request, be it a query parameter, or a form field, it turned that into a variable that we could then directly use inside of our application. So these variables would just tend to appear out of nowhere. And then of course, there's no validation there because things weren't there available for us. Well, we could validate, but it took a lot of extra time, which a lot of people didn't do. But, you know, we would make decisions based upon these flags that

Problems in Old PHP2:14

which a lot of people didn't do. But, you know, we would make decisions based upon these flags that would just come in from the request and then grant admin access. Wonderful. And then there's the database. We had these database functions. Of course, at the time, most of us used MySQL, but, you know, there was Microsoft SQL, there's so many other functions that we could use depending upon what database engine that we wanted to interact with. But, you know, here we would need to

upon what database engine that we wanted to interact with. But, you know, here we would need to connect to the database, then select the database that we wanted to work with. And then, you know, work with the form data, use it directly inside of a string so that then we could issue our query. And yes, horrible idea. But that is what PHP encouraged us to do. Again, we're talking about, you know, almost a completely different language. It's archaic. But, yep, SQL injection. It's what

you know, almost a completely different language. It's archaic. But, yep, SQL injection. It's what it encouraged us to do. And there's so many other things that were wrong with the language. Everything was in the global scope. We didn't have any kind of namespaces or anything like that. So as we, you know, included all of our files, they were all in the same level of scope and everything could get clobbered if that's what we wanted. And of course, it's not what we wanted,

everything could get clobbered if that's what we wanted. And of course, it's not what we wanted, but that's what happened. And then there were so many function names and parameter lists and things that, well, there just was no consistency whatsoever. It made working in PHP a very hard thing to do. And it wasn't just a little messy. It was, it was downright horrible. It was the perfect environment for introducing security bugs and an architecture that, well, if you've heard the term spaghetti code, PHP code was spaghetti code. It

perfect environment for introducing security bugs and an architecture that, well, if you've heard the term spaghetti code, PHP code was spaghetti code. It was awful. And it's the kind of code that taught pretty much an entire generation that PHP was bad. And really, that's where the whole misconception comes from. People remember the old PHP. And they haven't had any interaction or have been introduced to modern PHP. So they assume that Lara vel's foundation is still that, that horrible language. But PHP didn't just improve a

Modern PHP Improvements4:27

vel's foundation is still that, that horrible language. But PHP didn't just improve a little. It rebuilt the foundation. So let's look at some of the improvements. Again, that's such an understatement. But if we take a look at composer.json, we can see that we have this auto loading feature here. This is all based upon the PSR four standard. And any file that begins with a namespace of app is going to be pulled starting from that app directory. So if I take a look at

namespace of app is going to be pulled starting from that app directory. So if I take a look at my payment controller, well, here I use my stripe gateway class. And that file is automatically loaded. So I don't have to include it or require it or anything. It's just automatically done for me. So that I can create that gateway. And then I can do whatever it is that I need to do. And it's so normal now, it's boring. But really, you know, that's a good thing. But if we take a look

And it's so normal now, it's boring. But really, you know, that's a good thing. But if we take a look at that stripe gateway, we can see that this PHP class is very different than what we would have written, you know, 20 plus years ago. I mean, yes, you know, the basics are here. But of course, we have our namespace. So everything as far as this class is concerned is scoped to this namespace. But if we look at this method charge, what do we have? We now have scalar types, we

namespace. But if we look at this method charge, what do we have? We now have scalar types, we have nullable types, we have union types. So by using these type hints, we can provide clarity to anyone reading this code, anyone using this code, that this charge method accepts an integer value. And it returns a boolean. It doesn't feel like the same language as the PHP that people learned from, you know, some random blog 20 years ago, this is a modern language that has

Safer Structured Code6:12

people learned from, you know, some random blog 20 years ago, this is a modern language that has features that we would expect from really any modern language. So if we were to compare, not necessarily this entire code, but definitely this portion to where we get the username, the password, and we attempt to sign the user in, well, our code looks very different. Today, we would probably design a service that accepts our PDO object to interact with our database,

we would probably design a service that accepts our PDO object to interact with our database, so that whenever a user attempts to sign in, we pass the username and password, and we prepare a SQL statement, we aren't concatenating that value directly into the SQL statement, we are using SQL parameters. From there, we would then execute our query, not having to worry about SQL injection, we would then fetch the data, if we don't have a user, then well , we're not signed

SQL injection, we would then fetch the data, if we don't have a user, then well , we're not signed in. Otherwise, we verify the password and return the result of that, it's so much more structured, it's so much more mature, but then being able to use type hints to ensure that a string is passed for the username, a string is passed for the password, and this method returns a boolean, that gives us the opportunity to use tools that allow us to analyze our code so that we can find

Static Analysis Benefits7:33

that gives us the opportunity to use tools that allow us to analyze our code so that we can find problems early, before we even attempt to run it, tools like PHP Stan, or Psalm , they allow us to catch these kinds of problems almost immediately, therefore making us as developers much more productive. So judging Laravel by PHP's reputation from 2005 is like comparing a modern sports car with a horse and buggy. Yes, they were built to do the fundamental same thing as writing a web application,

horse and buggy. Yes, they were built to do the fundamental same thing as writing a web application, but they are two completely different things. Modern PHP is definitely a different thing than the PHP of yesteryear, because programming languages evolve and PHP's evolution has been very dramatic. In fact, Laravel exists today, frankly, because PHP became capable of supporting it. So the foundation isn't broken, it has been completely rebuilt.

So the foundation isn't broken, it has been completely rebuilt.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟