Classes vs Objects0:00
[♪ music ♪ All right, so moving on to objects. Now, in the last episode, you'll remember that I drew a distinction between a blueprint and an implementation of a blueprint. So in the programming space, we would think of classes versus objects. So a class refers to the blueprint, and an object refers to an implementation of that blueprint. All right, so let's use an example of a Playlist this time. So back in my day, we would create all sorts of playlists that we would burn to CDs.
This is just a simple explanation with no code.
You got to give it a good name that describes what kind of music you have on it. And, well, that would be the next thing, right? A Playlist consists of songs, right? What kind of behavior might the Playlist have? I don't know. Maybe we could shuffle the songs in the Playlist. Just reorder all of the songs so that each time we listen, you don't know what's coming next. Yeah, so we have some ideas here. All right, so now we should add our first class property.
Adding Class Properties1:14
Yeah, so we have some Idea objects here. All right, so now we should add our first class property. Now, think of it like this. Properties are just like variables for an object, okay? So in this case, if a Playlist consists of a name, then our variable, or let's use the term property going forward, is name, right? Next, I should add visibility for this property. But here's the thing. Visibility is a slightly higher level concept, and I think it'll just confuse you right now.
Visibility is a slightly higher level concept, and I think it'll just confuse you right now. So we're going to put a pin in this, and we will return to it later with a full video dedicated to the topic. For now, just accept that public means this property is publicly available to the outside world. If you have access to this object, then you have access to name. And sometimes that's good, and sometimes that's not good, and you want to disallow that access, all right? So we have a class.
Instantiating with new2:07
and you want to disallow that access, all right? So we have a class. We have a property. I now want to instantiate this class. And I can do so with the new keyword, new Playlist. So let's assign this to the variable playlist. All right, so now I'm going to drill this in for the third time, because I promise you it really does help to say something over and over and over and over. So this is our class. It's sort of like a blueprint, right?
So this is our class. It's sort of like a blueprint, right? This is our object, which is sort of like an implementation of that blueprint. And this implementation has a name of, what did we say, 80s headbangers? I'm an older millennial, come on. I like 80s headbangers. I can't help it. It's in my blood. It's where I come from. But yeah, we could just as easily have a playlist or a different playlist that is,
It's where I come from. But yeah, we could just as easily have a playlist or a different playlist that is, what did we call this? Depressing 90s. And we have a collection of songs that are unique to that playlist. That's the whole idea. For your blueprint, you can have any number of instances or implementations of that blueprint. That's the entire point. Okay, so now it might be nice if we could assign the playlist name right at the point when we instantiate it.
Using Constructors3:19
Okay, so now it might be nice if we could assign the playlist name right at the point when we instantiate it. And as it turns out, we can through the __construct method. And that's another new term. I'll show you how. I will copy this, and I'm going to pass it to our __construct like so. You'll see here my editor's squawking. And that's because we're trying to pass a parameter here, but it's not expecting one. So here's what we'll do. We create a new method, and we give it a magic name, which is __construct.
So here's what we'll do. We create a new method, and we give it a magic name, which is __construct. This is the constructor for the Playlist. And whenever we run new Playlist, this constructor will be called immediately. In fact, let's have a look right now. Let's say die('Hello there'). And now let's run this code directly in my editor. We run php on the current file. We instantiate Playlist.
We run php on the current file. We instantiate playlist. The constructor is automatically called. We die hello there. And sure enough, it is working. That's great. Okay, so now, again, remember that we are designing this. We are in charge of what we accept and what we work with. In this case, we have decided that we can pass through a name to the constructor. So now I can accept that.
In this case, we have decided that we can pass through a name to the constructor. So now I can accept that. And just to show you incrementally, if we die and run it, yep, we accept the name. Okay, so now what I'm going to do is take this name that we are passing in, and I'm going to assign it as a property on this object. We do it just like this. $name equals what you pass to the constructor. Okay, so now let's do this. Let's die and var_dump the playlist and have a look in my terminal. Once again, php on the current file.
Let's die and var_dump the playlist and have a look in my terminal. Once again, php on the current file. And there we go. We have an object or an implementation or an instance. We can sort of use those interchangeably, by the way. And this one has a name of depressing 90s. Very cool. So let's do this. Maybe we have a playlist array, and we will push to that array. So we'll say playlist push a new playlist called depressing 90s.
Maybe we have a playlist array, and we will push to that array. So we'll say playlist push a new playlist called depressing 90s. Let's do another one that is 80s headbangers. Let's do another one that is, I don't know, boy band faves, if you want. And yeah, now it's boy band dash two words. What do we do there? Anyways, let's die and var_dump our playlist. And you can see the way this works. One more time. Let's clear it.
One more time. Let's clear it. php on the current file. And if I expand this, I have an array that consists of three playlists. And each one shares the same DNA or structure, but they are individually unique. Depressing 90s, 80s headbangers, boy band faves. You get the idea. It's pretty cool. Okay, so now let's do one final thing before we wrap up. A playlist is not a playlist if it doesn't consist of songs.
Adding Songs Data6:08
Okay, so now let's do one final thing before we wrap up. A playlist is not a playlist if it doesn't consist of songs. So maybe when we instantiate it, we can optionally pass through an initial set of songs. All right, so let's do that now. Songs. So once again, I will write to a property, and then I will declare it at the top. Public songs. So let's simplify this just a little bit. We'll do one playlist for the time being. And this will be the 80s headbangers, just because I can think of songs off the top of my head.
We'll do one playlist for the time being. And this will be the 80s headbangers, just because I can think of songs off the top of my head. All right, so my initial array of songs might be back in black. What else? Are you ready? Maybe this is my ACDC-specific playlist. Hells bells, highway to hell. You get the idea. All right, so now I'm instantiating playlist with a name and an initial set of songs. So this time, if we run it, there we go.
All right, so now I'm instantiating playlist with a name and an initial set of songs. So this time, if we run it, there we go. Here's our object. It consists of a name and a set of songs. All right, so now we've thought about data. But what about the behavior side? Well, what about shuffling the playlist? What about restarting the playlist? What about sharing the playlist with others? What about publishing the playlist, right?
Adding Shuffle Behavior7:29
What about sharing the playlist with others? What about publishing the playlist, right? So maybe if I added behavior for shuffle, what would we do there? Well, we would just shuffle the array of songs. So what I could do is, in fact, defer to php's native shuffle function, not to be confused with our method name. And I could just say shuffle this songs. And that's destructive, so I don't have to reassign it to the property. It'll just take effect. All right, let's give that a shot.
It'll just take effect. All right, let's give that a shot. All right, so let's do this. We'll say, give me the first item or the first playlist, and I will call a shuffle method on that object. All right, so now if I run it, let's see, we have highwayToHell at the start. If I run it again, let's just keep doing it a few times. Of course, highwayToHell is first four times in a row just by chance. But yeah, you can see that we are properly shuffling that array when we call that piece of behavior, which is really cool.
Constructor Property Promotion8:24
But yeah, you can see that we are properly shuffling that array when we call that piece of behavior, which is really cool. OK, so now we're going to wrap up with one last thing. I'll comment this out. This is sort of the traditional way that we can pass through constructor arguments that we then assign as properties. But as of PHP 8, we can allow for something known as constructor property promotion. And it's just kind of a simplified shorthand that we can reach for instead. And it's what I would generally recommend to you.
And it's just kind of a simplified shorthand that we can reach for instead. And it's what I would generally recommend to you. So this is not deprecated or discouraged or anything of the sort. It's just a more traditional way to do it. Instead, I could declare the visibility directly on the parameter like this, public $name, public $songs. And if I do that, you know what? I no longer have to manually assign it or declare the properties. So yeah, there's no functional difference here. It works exactly the way it did before.
So yeah, there's no functional difference here. It works exactly the way it did before. But less code is always a good thing in my book. All right, so I think we're making pretty good progress, actually. At this point, you've now learned about classes and objects or implementations or instances or every synonym in between. You've learned about magic constructors and constructor parameters and property promotion. We're doing really well, I think. So as always, if you are subscribed, be sure to take the exam below this video.
We're doing really well, I think. So as always, if you are subscribed, be sure to take the exam below this video on the Learncast website. Otherwise, I will see you in the next episode, and we'll keep digging in.
