August 202310:48223.2k views

PHP doesn't suck (anymore)

About

PHP in 2023 is very different than PHP in 2012. Let's run through some of the changes!

Transcript

Everything I said

00:00

Every time I make a video on PHP, which is basically all the videos I make, I get the exact same comment. PHP is dead. PHP sucks. All the time, every time. And most of the people that think PHP is dead haven't looked at PHP since, like, twenty twelve, which is before five point four came out. And listen, things were a lot different in twenty twelve.

00:22

I had just graduated college with a degree in accounting for some reason. This guy was still the president over here. This movie was in theaters, and honestly, pretty good movie. Our music went through this real weird hey ho phase where we were always shouting hey ho in the songs. Hey ho. Hey.

00:43

So I think it's safe to say that a lot has changed since twenty twelve. I'm no longer an accountant, and PHP no longer sucks. I'm gonna run you through a bunch of the language changes that have happened since around twenty twelve when PHP five point four came out, and stick around to the very end because we're gonna talk about types. We gotta talk about type safety. Every time I talk about PHP, people just say, "Yeah, I just need types.

01:05

We gotta have types. What about types? Do you guys have types? I can't leave my types." We've got types. So I'm gonna show you all the different types you can do at the end. Let's take a look at these changes. Going all the way back to twenty twelve for this one, PHP five point four included traits. So instead of writing the same method body over and over and over, or instead of abusing inheritance, we now have composition.

01:28

So you can have traits and include those in every class, favoring composition over inheritance. Short array syntax. Imagine writing out the word array like a caveman when you can just use square brackets. Short array syntax allows you to use square brackets. Thank you very much. We now have array destructuring.

01:49

It used to be that you would have to assign an array to a temporary variable and then reference those elements by index if you wanted to assign them to other variables. Now, you can just use array destructuring and directly assign the variables from the array. We have first class variadic functions, so you can pass as many arguments to a function as you want using this dot dot dot syntax.

02:12

Speaking of dot dot dot syntax, you can use that same syntax to spread or splat an array into an argument list for a function call. You can use that same syntax to spread or splat an array into another array. So if you wanna take all the items from one array and put it into a second array, you can do that with this spread operator.

02:33

We now have generators. If you want to do something memory intensive but in a memory efficient way, you can use a generator. So if you need to read a huge file, you don't have to load that whole file into memory. You can use a generator to yield it line by line by line. If you ever need a new class but can't be bothered to make a new file, who has the time, you can create an anonymous class.

02:55

An anonymous class can implement an interface just like every other class, and so this is a great use case for when you need one stupid little implementation, but you don't wanna make a full class for it. This is a big one. I need you to pay very, very close attention. We now have trailing commas in function calls. So look very closely at email.

03:15

Look at the very end of the word email and watch what's about to happen. Amazing. We now have trailing commas in method calls. PHP rules. This one's for all my JavaScript friends. I know you love your arrow functions. Well, look out friends. We've got arrow functions too. Ours honestly are, well, they're, they're a little bit worse.

03:37

I don't like the FN at the beginning to denote that it's a function, but hey, we work with what we've got. We have short closures or arrow functions. PHP seven brought us the null coalescing operator. So we can say the username is equal to the username unless the username is null, in which case we'll go with guest.

03:58

So we have this nice, neat null coalescing operator, but we also have the null coalescing assignment operator, which can shorthand all of that and say, "If the username is set, leave it alone. Otherwise, set it to guest." Speaking of nulls, we used to have to do something like this. We would pull the birthday off of the user, and then before we were allowed to call a method on birthday, we would check to see if birthday existed, because you don't wanna call a method on null.

04:25

But now we have the null chaining operator, and we can just say, "If birthday is not null, call the format function on it. If it is null, just stop the chain right there." We have named arguments now. It used to be that if you wanted to skip over an optional argument, you had to leave a null in the position of that argument. But now we can just name our arguments and pass them in any order that we want.

04:47

PHP has attributes now, which in other languages are probably called annotations, but you can add an annotation to a class or a method or an argument or a property. You can add an annotation to almost anything and control a little bit of behavior that way. Keep this in mind. We're gonna look at this again in a second. This one is just a huge, massive improvement.

05:08

It used to be when you were catching an exception, you had to have a variable for that exception even if you weren't gonna use it. Even if you were just simply gonna return false, you had to have that little e in there. Now, look at this. Watch this. Watch that e. Amazing. No longer needed. If that's not reason enough to switch to PHP, I don't know what to tell you. Okay. Remember those attributes that I told you about literally two seconds ago?

05:31

There's an attribute that will mark an argument as sensitive and redact it from all stack traces so you don't accidentally end up logging something you shouldn't at a language level. Very cool. Look at this switch statement. It's hideous, it's huge, we hate it. What we're doing is we're switching on a status code, and then in the body of every status, we're setting a message variable.

05:52

There's gotta be a better way. There is a better way. We can use the match statement now and assign the message variable to the result of the match statement. And look, at two hundred, three hundred, we can have multiple matches return the same thing. This is a much more compact, much more readable way to write a switch statement that assigns a variable.

06:13

We've got weak maps now. Did you know that PHP has weak maps? You can set an object as a key in a weak map where you can't, you can't do that in an array. And also, weak maps are way better for memory because they'll get garbage collected when there's no references to that object anymore. PHP now has enums, so you can have an enum class where you delineate the cases inside the body of that class, and those enums can also have a value.

06:37

So if you wanna use the words draft, published, and archived in your code base, but you wanna use the values one, two, and three in your database, you can do that with a backed enum. That's not all that enums can do. You can add methods to your enum classes. In this case, we'll add a method that gives a user-friendly display to each of the enums, and we're using the match statement again, and we're just matching against this, and all of our cases are our enum cases.

07:03

One of the great things about having first-class enums is you can now use them as type hints. So if a method is expecting an enum, you can hint that enum class, and that way PHP will validate that the incoming argument is that enum. All right, here we go. We're gonna run through a bunch of the ways you can add types to PHP. First, you can have typed arguments.

07:25

In this case, we're saying A and B must both be integers. This is a scalar type hint. You could also type hint classes or enums like we showed earlier. You can have return types. In this case, we're saying it must also return an integer. That makes enough sense. If you're adding two integers together, it should return an integer. But not all numbers are integers. Some numbers are floats.

07:47

So you can also have union types. You can say int or float, and it can return an int or a float. If you wanted to add more than two numbers together and you have a variadic function, you can also use types for that, no problem. Let's move on to a class. Here we're saying that the argument input must be of type Foo or Bar. This is the union type that we just talked about. But you can also have an intersection type where you say that the argument input must be of type Foo and Bar, which is really helpful when you're trying to ensure that an argument adheres to more

08:17

than one interface, but you don't care what the type of object is. As long as it adheres to Foo and Bar, you can do whatever you need to do. You wanna get even crazier? If you wanna ensure that input is Foo and Bar or it's nullable, you can have a disjunctive normal form type, and I had to learn those words for this specific video.

08:37

You can have Foo and Bar or allow it to be null. Let's look at another example. We've been typing arguments, but you can also have typed properties. So if you look at this class, you'll see that the public title property is of type string, and the public date property is of type date time. Now, this is a little repetitive, isn't it? We've got the two properties at the top, we've got the two arguments in the constructor, and then in the constructor we have to set the class level properties.

09:03

This is a little verbose. This is how we used to have to do it until the PHP gods introduced constructor property promotion, and now you can put the visibility in your constructor signature. You can say public string title, and it will end up as a public property on your class. This is called constructor promotion. I don't love that you end up with an empty constructor body, but I do love that it reduces all of that boilerplate.

09:27

If you wanna mark a property as read-only, meaning that it can only be initialized once and then never changed, there's a keyword for that. You can say that a property is read-only, and you can do that in the constructor as well. If your entire class is read-only, you can mark the class as read-only instead of marking each individual property as read-only. A lot of stuff has changed.

09:48

If you haven't been paying attention, you might think PHP is dead, but alas, it is not dead. Let's talk about speed. Every time I make a video, people are like, "Yeah, but PHP is so slow." PHP at between, like, five point six and seven experienced a four hundred percent performance increase, and then I think another twenty percent between seven and eight, so PHP is not as slow as you remember.

10:11

So if you need something like fifty thousand requests per second, you should probably still-- You should probably still not use PHP for that. But look at me, listen to me. When have you personally needed fifty thousand requests a second? How many users do you have? So PHP is fast enough. It is fast enough for most of your use cases, and if you need a specialized use case, use a specialized language.

10:36

But the PHP of yore is not the same as the PHP of today. It is much, much faster. It is much, much better, and it is not dead. See ya.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues