Introducing PHP Classes 0:00Okay, so here's the deal, bear with me. You learned a bit about working with MySQL, right? And now you want to switch over to your PHP and figure out, well, how do I fetch those records from the database and display them within my HTML or my Vue? I want to show you that, but there's one other concept that you at least need a very, very basic understanding of, and that concept is classes. So let's do this. Here's how we define a class, and then I'll explain it a bit more. A class can represent any possible thing in your project or in your domain. And we often say, well, look for the nouns.A class can represent any possible thing in your project or in your domain. And we often say, well, look for the nouns. So the most common example is person. Person is a noun. But in the context of what we're working on, maybe like a little to-do application, the class could be task. So think of it as the representation or the blueprint for some concept in your application. And don't worry, this is confusing. It's going to take you some time to really understand what classes are, how they communicate with each other, and why you would use them.It's going to take you some time to really understand what classes are, how they communicate with each other, and why you would use them. I totally get it. It could take over a year. So for now, I really just want you to understand the syntax and how you work with it. Okay. So here's how we define a class. Class and then you give it a name. And like I said, at least initially, think of nouns, and that can be a good start. Once you get a little more seasoned, you'll learn lots of other use cases for classes.And like I said, at least initially, think of nouns, and that can be a good start. Once you get a little more seasoned, you'll learn lots of other use cases for classes. But for now, pointing out the nouns is a good start. So you could have a class. You could have a user in your system. You could have a product if you have an e-commerce site. So think about whatever it is you want to build and then try to spot the nouns. So like I said, in our case, if you're building a to-do application, well, your nouns might be a to-do or a task. It might be a comment, like maybe people can comment on your tasks.be a to-do or a task. It might be a comment, like maybe people can comment on your tasks. So you might have one called comment. You might have users who can sign up and create their own to-do lists. So you might have a user. So notice how I'm spotting these. You're finding the important components of your application. And then the class will be the singular form of that, to-do, comment, user. So it's the representation for one thing. Okay. Defining Task Properties 2:07So it's the representation for one thing. Okay. So now if we have a task, I'm going to use task here instead of to-do, what would you need for a task? Well, you might need, like we said, a description. And here's a little tip. At least to start, you can look at your database columns. So we decided that a task needs a description as well as whether it has been completed or not. Now in real life, yeah, you'd have a lot more columns here.not. Now in real life, yeah, you'd have a lot more columns here. You'd probably associate it with the user. You might associate it with a category or a project. But for now, just a description and whether it has been completed. So here's what we can do. I'm going to create a function just like you learned here, exact same syntax, but store it within the class. Now as you'll find, when a function is defined within a class, you'll hear others refer to it as a method.Now as you'll find, when a function is defined within a class, you'll hear others refer to it as a method. But it's still essentially the exact same thing. But yeah, just as a rule of thumb, call it method if it's defined in a class. If it's not, then you can call it a function. But still, same thing. Okay. We're going to use this unique term. Like I said, I'm throwing a lot at you here, so just take it one step at a time. This is a special method name. Constructors and Instantiation 3:15Like I said, I'm throwing a lot at you here, so just take it one step at a time. This is a special method name. So underscore, underscore, construct. You can think of that as sort of like your constructor. So a constructor is something that will automatically happen whenever you prepare a new task or a product or a user. So I will say automatically triggered on instantiation. And that's the word we would use here. You instantiate a task. And how do we do that?You instantiate a task. And how do we do that? You do that with this new keyword. So I could say new task and save that to a variable. Now you see this right here? Any arguments that you pass through here can be accepted in your constructor. So this would accept A, B, and C. Make sense? So if we've decided that a task consists of a description and whether it has been completed, well, let's give it a description.So if we've decided that a task consists of a description and whether it has been completed, well, let's give it a description. Go to the store. And completed? Well, if you think about it, every task is not completed when you create it, right? So maybe we can do that automatically. Okay, let's do this. Description. And then I will assign it to this object. So this new keyword here, this description, is equal to whatever the user passed in rightAnd then I will assign it to this object. So this new keyword here, this description, is equal to whatever the user passed in right here on instantiation. Okay. So we are setting a property here. And notice this new arrow syntax. Yet another new thing here. We can refer to the current instance of this class, which is what we would call an object, by the way. An instance of a class is an object.by the way. An instance of a class is an object. And we can assign a property to this object using this syntax. Dollar sign this, arrow, and then the name of the property, which we will define up here. Okay, but now notice these keywords, public and protected. Yeah, this can be very confusing, especially in these early stages. So I'm intending for you to be incredibly confused at this point. And if you are, like I've said before, you are entirely normal. This is, well, we use the term encapsulation for this. I don't want you to think about it really at all right now.This is, well, we use the term encapsulation for this. I don't want you to think about it really at all right now. Just for now, accept that these keywords are how we define visibility. So who that is not part of this class can access this property or this method. But yeah, for now, don't even worry about it. Okay, so we also said we needed a completed property. But now, like we said, we could accept it here, but a task is always not completed when you create it, right? If I create a new task in my to-do list, well, it's not completed yet. So I'm going to assign this automatically to false.If I create a new task in my to-do list, well, it's not completed yet. So I'm going to assign this automatically to false. Okay, so I threw a lot at you, but this is the basic structure for a class. Class, give it a name, and then within braces, you can define properties. And for now, just set your properties to protected. So we are protecting their visibility from the outside world, which means I can't do things like this, task description. And the reason is, well, you're trying to access a property that is protected. It is protected from the outside world. Now, if it was public, you're saying, yeah, we can publicly share this property with anyone.It is protected from the outside world. Now, if it was public, you're saying, yeah, we can publicly share this property with anyone. We don't care. So in this case, you could access it. And there's lots of situations when you would do that. But for now, we're making it protected. Okay, so let's do this. Let's var dump the task and just see what we have here. Okay, so if we switch over to Chrome, I have a little tool here that will clean up the output.Okay, so if we switch over to Chrome, I have a little tool here that will clean up the output. But don't forget, you have this functions file. So you could do dd if you import your functions.php file. All right, so change that out, give it a refresh, and now we can see your task. Okay, so the output is a protected property called description that contains this value, and then another protected property called completed that is set to false. So now here's the cool thing. You can add any number of functions or methods within this class to help you interact with it. Adding Class Methods 7:24You can add any number of functions or methods within this class to help you interact with it. So for example, public function is complete. Notice we're using these nice readable terms. So think about it. If you ever wanted to figure out if the task is complete, well, try to make your method names or your function names line up with the way you would speak it out loud. Even if you're in your office or your bedroom, speak it out loud and then try to get the code to match up with how you say it. So I said task is complete, so that is the name of the function or method that we wantcode to match up with how you say it. So I said task is complete, so that is the name of the function or method that we want to call. Okay, so how can we determine if this one task is complete? Well, can we just check this section right here? Okay, return this object, arrow, completed. And that's going to return true if it is set to true or false if it is not complete. All right, so let's try it. I'm just going to var dump in this case. Var dump and tell me if the task is complete.I'm just going to var dump in this case. Var dump and tell me if the task is complete. Ah, false. It's not. However, why don't we add something to our API, you might say, to complete the task. So you do something, you click a button, and then behind the scenes you want to complete the task. Well, once again, I'm using the term complete, so I will call it like that. Task complete. All right, let's add this new method.Task complete. All right, let's add this new method. Now, you'll see me manually writing this out. It gets kind of clunky. So research your editor and see if it supports something called snippets, in which case you can just do something like this, met tab, which is what I have defined, and then it will build up that code for you, and that way you don't have to type it out over and over. Okay, so to complete the task, what would you do here? Well, really, we're just updating this property from false to true.Okay, so to complete the task, what would you do here? Well, really, we're just updating this property from false to true. So maybe this, this arrow completed now equals true. Okay, so let's try it. We instantiated a task, so we now have a new task object is how you would refer to it. And then we did something and later completed the task, complete the task. And now our call to task is complete should return true rather than false. Refresh, and now it's set to true. So now, why don't we group this with some other concepts you've learned? You know about arrays, right? Rendering Tasks in View 9:50So now, why don't we group this with some other concepts you've learned? You know about arrays, right? So why don't we set up an array of tasks, like this? Tasks equals an array of new tasks. Go to the store, and then another one. Finish my screencast. Clean my room. Okay, so you have a tasks array that consists of three items, each of which is an instance of task. So if we now die and dump our tasks array, let's come back and refresh.of task. So if we now die and dump our tasks array, let's come back and refresh. Aha, you have an array of three objects, once again, each one, which is called task. So now notice when I said that a class is sort of like the blueprint. So we have our blueprint here, but each instance of that class can be composed of different values and different properties. So this task has a description of go to the store, but that task, even though it uses the same blueprint, has a totally different description, and the third task is the same as well. That's sort of what I mean when I say blueprint.as well. That's sort of what I mean when I say blueprint. Okay, so now we have an array of tasks. Why don't we load up our view, like we did down here? Let's clean this up just a touch. So at the top, we don't need functions right now, but we define a class. We set up an array of tasks. We load a view. And now within here, I'm going to say PHP for each tasks as task. We're going to use the shorthand like we learned earlier.And now within here, I'm going to say PHP for each tasks as task. We're going to use the shorthand like we learned earlier. And then why don't we just spit it out within a list item. So we will wrap this within a UL. And then my list item will be PHP echo, or like you learned, we can do less than question mark equals. And now how can we grab some of these values? Well, we can do it in two ways. Remember how I said by making this protected, we are protecting it from being accessed by the outside world?Remember how I said by making this protected, we are protecting it from being accessed by the outside world? Well that means you can't do this by default if the property is set to protected. So if we go back to Chrome, I think you're going to see an error. Yes, cannot access protected property, right? We are protecting this property from being accessed from the outside world. But now if I set this to public, we are making it publicly visible to the outside world. So we can access those values. Is that starting to make a little bit of sense? And you might be wondering, well, why would you ever want to protect it?Is that starting to make a little bit of sense? And you might be wondering, well, why would you ever want to protect it? Like I said, that's kind of a higher level thing that I don't want you to worry about too much just yet. And we're still trying to figure out the basics here. So for now, it's okay to understand this at a very light level. That means if we want to make these public, no problem. Another option would be to keep them protected and then access them through methods like this, description, and then return the description. And once again, there's a number of reasons why you might want to do it in this way.this, description, and then return the description. And once again, there's a number of reasons why you might want to do it in this way. But we're not going to get there just yet. So I'm going to keep these as public. And now when we come back, this should all work. Why don't we do one other thing? If the task is completed, maybe we want a strikethrough. So let's do this. We'll say, PHP, if task completed, then we're going to have a strikethrough here. And then basically reproduce this down below.We'll say, PHP, if task completed, then we're going to have a strikethrough here. And then basically reproduce this down below. Okay. So does that make sense? If the task is completed, then we want to wrap the description of the task within a strike. So we set up the opening tag, and then at the bottom, we set up the closing tag. Now this will work, but we can also do it in another way. So first, let's check it. I refresh.So first, let's check it. I refresh. No tasks are complete, so we don't see anything. Why don't we complete a task? So we'll do this. How about, go to the store is done. Let's just fetch it. So it's the first item within the array, and all arrays begin at zero. So the very first item is what we call the zero index. The next index is one, and the next index is two, and so on and so forth.So the very first item is what we call the zero index. The next index is one, and the next index is two, and so on and so forth. So if I want to access this particular task instance, we could grab it using tasks zero, and then we're going to say complete. Okay. So now, if I refresh, this first one is complete, so we see a strikethrough. Does that make sense? Good. Now, I noted we could clean this up, or at least change it a little bit. One option is, well, if the task is completed, let's just put it all within here like this.Now, I noted we could clean this up, or at least change it a little bit. One option is, well, if the task is completed, let's just put it all within here like this. That way, I can get rid of this section entirely, and then do something like this. Else, just display the description as it is and close it out. Okay. So if I give this a refresh, you're going to see the exact same thing, but it's just another way of doing it. So if we have a completed task, here is how we want to output the description. Otherwise, else, it's not completed, so just display the description as it is, and we're done.Otherwise, else, it's not completed, so just display the description as it is, and we're done. Okay. So we're going to finish up for this lesson. I expect this to be confusing stuff, okay? So just know that you create a class like this. You can set properties. You can create a special construct method that will be triggered automatically any time you say new and then the class name. Next, any arguments you provide here will be sent through to the constructor. Transition to Database Fetching 15:32you say new and then the class name. Next, any arguments you provide here will be sent through to the constructor. You can then accept those and assign them to properties like we did here. And then finally, you can add any number of methods to interact with whatever data or behavior you're allowing. Okay. So with classes 101 out of the way, in the next lesson, let's go back. So you've learned a little about MySQL. You've learned a little bit about classes. So now let's use a class, an existing class that PHP provides to fetch data from our MySQLYou've learned a little bit about classes. So now let's use a class, an existing class that PHP provides to fetch data from our MySQL database.