From Procedural to OOP 0:00As awesome as Laravel is, it does assume that you have a basic understanding of object-oriented programming. So if this whole world is still a little blurry to you, that's certainly okay. All of us were there at one point. But before you move on to Laravel, I do recommend going through this series to brush up. So why don't we start at the very beginning. I'm going to assume that you're coming possibly from a more procedural world. The type of world, for example, where you'll just place a bunch of PHP right above a block of HTML. You have code here that connects to a database and then builds up a collection.of HTML. You have code here that connects to a database and then builds up a collection. And then you come down here and reference the variables that you just created above your HTML. I'm betting that this is something that looks pretty familiar to you. And once again, I know this because everyone goes through that phase. You just have tons of procedural code here. No real separation of concerns. We are repeating ourselves over and over. None of this code can be reused elsewhere. Creating a Basic Class 1:01We are repeating ourselves over and over. None of this code can be reused elsewhere. It all just ends up being a big nightmare. So let's fix that. Step one is to review a basic class. How do you create one? What is the purpose of one? What does all of this look like? Well, why don't we use the obligatory example of building some kind of task app. We're not actually going to build that.Well, why don't we use the obligatory example of building some kind of task app. We're not actually going to build that. I'm just going to show you what one class in that system might look like. So let's create a task.php. Now why did I call it task? Well, basic object-oriented teachings tell us to hunt down the nouns. Now here's the truth. In the long run, that sort of thinking ends up hurting you. But at least for now, where we're still just at step one, it's okay to think of it in that way.But at least for now, where we're still just at step one, it's okay to think of it in that way. Hunt down the nouns. So if I'm building a task app, it makes sense that I would have a task class. And you can sort of think of this as the blueprint for what a task might look like. For example, a task probably has a description, right? Well you're used to doing it like this. Description equals and then whatever you set there. But now I want you to think of this less as a variable and more as a property. And in this case, we are making it public. Instantiating and Accessing Properties 2:17But now I want you to think of this less as a variable and more as a property. And in this case, we are making it public. Don't worry, we will cover encapsulation quite a bit in follow-up lessons. But just for now, accept that when we write public, that means outside of this class, anyone can access this property. For instance, let's hard code a description here. Go to the store. All right, so if I simply want to access this value, how do I do it? Well, we create new instances of these classes. In those instances, we would refer to as an object.Well, we create new instances of these classes. In those instances, we would refer to as an object. So here's what that might look like. Task equals a new task. That's what that looks like. So now for example, if I want to access this description property, I can say task description. And you know what? Why don't we just var dump this and view it from the terminal? We can do that by simply running PHP on the task class. And there you go.We can do that by simply running PHP on the task class. And there you go. But now I used the term blueprint, but we've hard coded a string in here. So that doesn't seem right. That's sort of like saying the blueprint of this house has a red door. Well, maybe some houses have red doors. Or if we were to translate that to code, some instances of that blueprint have a red door, but some have blue and some have brown. So if this is our blueprint, so to speak, then we can have multiple objects that have slightly different makeup. Using Constructors for Setup 3:47So if this is our blueprint, so to speak, then we can have multiple objects that have slightly different makeup. Let's see what that looks like. I'm going to remove this entirely, and now I'm going to add a new special method here. Now on that note, I said method, not function. So you may know when you create regular old functions, well, we just call it function. But within the context of a class, we refer to them as methods. Methods on an object. So what does this method do, construct? And by the way, notice I'm doing underscore, underscore, construct.So what does this method do, construct? And by the way, notice I'm doing underscore, underscore, construct. Well, this is a special method that will be called immediately when you instantiate a new class. So think of it like this. Whenever you say new task, the contents of this method will automatically fire. Let me show you. Bar dump, called. And now if we run it again, you'll see that we hit that method. But even further, when this method is called, it will receive any optional arguments thatAnd now if we run it again, you'll see that we hit that method. But even further, when this method is called, it will receive any optional arguments that you pass through. So why don't we add our description right here as the first argument. Learn OOP. Now I've passed an argument here. That means it'll be available right here. Let's try that out. Bar dump the description. And if we run it again, now we get learn OOP.Bar dump the description. And if we run it again, now we get learn OOP. But notice that I got null after that. And that's because, well, we're trying to access the description property, but it is null. We haven't set it to anything. So that means I need some way to say I want this value to be assigned to the description property. How do we do that? Well, we use a syntax just like this.How do we do that? Well, we use a syntax just like this. And we're going to go over this together. So let's run it again. And there you go. Now what just happened here? Well we passed learn OOP through. That is now represented as the description argument. And then I said I want to assign that value to the description property. So what the heck is this right here?And then I said I want to assign that value to the description property. So what the heck is this right here? Well think of this as a way of referring to this object, or more specifically, this instance of our class. So now once we instantiate task, we assign learn OOP to the description property. Then when we wish to fetch that value, we can simply call task description. And that's why we get the output there. Now remember when I said that you could have multiple instances or multiple objects? Well let's see what that looks like. Let's say we have a task two, a different task.Well let's see what that looks like. Let's say we have a task two, a different task. And we will instantiate that. And why don't we say pick up groceries. Okay so we have a new instance here. But if I come back and run it again, you will still see learn OOP. And that's because for this first instance of task, the description is set to learn OOP. But now for this second instance, the description is entirely different. And this is exactly what I mean when I use that term blueprint. So now if I say task two, and we run it, now of course we'll get pick up groceries. Adding State and Methods 7:05And this is exactly what I mean when I use that term blueprint. So now if I say task two, and we run it, now of course we'll get pick up groceries. Next what about the process of completing a task? It stands to reason that a task will either be finished or not finished. Or completed or not completed. So maybe, and once again I'm going to stick with public here because we haven't yet reviewed a term called encapsulation. I will say public completed and by default we will set that to false. When you create a new task, well obviously it's not done yet because you just created it so the completed property is set to false.When you create a new task, well obviously it's not done yet because you just created it so the completed property is set to false. Now if we take a look at that, and I will remove that, task completed, and if we run it, sure enough that returns us to false. So how might we represent the process of marking a task as complete? Well here's an important thing to think about. What would be the most readable and speakable way to represent that? Well maybe it could be something as simple as task complete. But now we are referring to a method, not a property. So how does that work?But now we are referring to a method, not a property. So how does that work? Well remember, methods are just the term that we use for functions. So you should already know how to do this. Complete. And now we still have this issue, how do I change completed to true in this case? Well we do it exactly the way we did up here. This completed equals true. So let's see if that worked. We create a new task, and then at some point in our application's lifecycle, we mark thatSo let's see if that worked. We create a new task, and then at some point in our application's lifecycle, we mark that task as being completed. And now if we var dump taskCompleted, and if we run that, we get true. So I'm hoping if all of this stuff is new to you, you're starting to see that really it's not the most complicated thing in the world. Naturally, there are some terms that you need to understand, like inheritance and composition and encapsulation, and then higher up you'll learn a little bit more about messaging, and then terms like polymorphism. All of this stuff definitely feels and seems confusing from the outside, but if you work Wrap-Up and Assignment 9:18then terms like polymorphism. All of this stuff definitely feels and seems confusing from the outside, but if you work through each of the videos in this series, I think you're going to find it's just not the most complicated thing in the world. So keep playing around with this. In fact, here's a little assignment. Right now a task has a description, but that very well could be a long-form description. So why don't you also offer the option to pass through a title? That way we could say, new task, learn OOP. And then if you need to provide some more information, the teacher blah blah blah, stuffThat way we could say, new task, learn OOP. And then if you need to provide some more information, the teacher blah blah blah, stuff like that, then you could do that as a second argument. This is the title. This is the description. Okay, have fun. And once you understand everything we did in this video, move on to episode two.