 Hey everybody, this is Brian. Welcome to the 30-second LAMP tutorial. Today we're going to be discussing classes. Now, what's all this object-oriented stuff and why do we even need it? Well, if you've been following along, you know the difference between a class and an object. But you don't know really why we need these things. Well, so far we've been doing procedural-based programming, meaning you write a procedure and the compiler executes it. But what happens if you want to do something rather complex? Like you've got 100 employees in your company and you want to know the names of each one. Well, obviously you make an array. What if you want to also know their age? Well, multi-dimensional array. But what if you also want to know their employee ID number? Well, now you have a big multi-dimensional array. What if you also want to embed some sort of logic in there so you could split the name into first name, last name, things like that? And you see how programs can get very complex very fast. So this comes object-oriented programming to the rescue. And you see me typing some code. Don't worry if you don't understand what any of this stuff is just yet. All we're doing here is we're just making our blueprint because that's what a class is, is a blueprint. And I'm still myself getting familiar with the PHP syntax. I'm very used to C-sharp, C++, Java, Visual Basic, things of that nature. PHP is a little bit different, but not too bad. So here is our blueprint, our class. Got our keyword class that denotes that this is a class. Here's our code block of the class. And then inside the class we have some things going on. We have two variables, name and age. Notice how they have this public in front of it. So this is function. Well, this is an access visibility modifier. You just say visibility for short. What we're saying is it's public, which means it's available to the public. Things can be public, private, or protected. We're not going to really go over those in this tutorial. That's for a future one. But you just know that things can have different levels of visibility. So for example, you could make age private, so nobody knows your age. I think women love that, don't they? And then we have our function and we're just simply saying hello. Now we want to create an instance of this class. That's right. We haven't actually made a class. We just made a blueprint of the object. Did I just say we haven't made the class? I was wrong. We've made the class. We made the blueprint. We haven't actually made an object or an instance of that class. Don't get confused. So we're going to say e1 equals new. You guessed it, person. And then we want to say e1. Notice how this menu pops up. Very helpful. But that's what a class is. It's a blueprint. And we're creating an instance of this blueprint or an object. Now we have properties and functions and methods and all that good stuff in there. So let's just say name equals. And we can set the name. Brian. And let's say e1. If you're wondering what this funky little arrow thing that I'm doing, the slash grader or the dash grader then. But that's kind of a whole loader from C++. If you know C++, you're sitting here going, aha, that's a pointer. You're absolutely right. That's a pointer. What we're doing is we're making an instance of that class or an object, throwing it in memory, and PHP holds a pointer to it. If you have no idea what a pointer is, don't worry, it's well beyond the scope of these tutorials. You'll never even need to know what they are. You just need to know how to work with them. But think of that arrow as saying we're pointing to something. There we go. Notice how you have to have the parentheses in there in order to call the function. Just like normal PHP, you've got to have the semicolons. So let's break this down super quick. We have our class, our blueprint, and we're creating an instance of it. Here's our class. We're saying this variable is a new person. So we're making a new instance of that blueprint, shoving it into memory. Now we can work with it as an object, because we have an instance. And we're saying instance, point to, and then name, which is the name variable. And then point to age, the age variable. And then say hello. And notice how we can actually manipulate and change things. Let's modify this hello a little bit. There is another variable you should know called this. Now what this is, it's a pointer to the current object. Hello. My name is this name, and I am years old. Now let's look at this real quick. What are we doing? Oh my goodness. All right, we're saying hello, my name is, and then this, the pointer to the current object, in this case the person class, name, and I am this age, years old. And it spits out hello, my name is Brian, and I'm 38 years old. So that is our object. Let's do another one real quick here, just to show that the real power of object-oriented programming here. Change these, and we're going to say, Heather, who is 20. Now we have two instances of the same class, or I should say two objects. Notice how person has not changed. Here's our blueprint, nothing's changed. But it does two different things. That's called a polymorphic algorithm, by the way. Hello, my name is Brian, I'm 38 years old. Hello, my name is Heather, and I'm 28 years old. Notice how the code is exactly the same. But based on the inputs we give it, or the variables, we get different results. Now that, in a nutshell, is classes and objects. How to create them, how to use them, and how to work with them. What you've learned is what a blueprint is, or what a class is, how to make things public, how to make variables inside. And you've also seen a little bit of variable scope, which we'll get into in another tutorial, where we're saying this is a global variable, meaning this is available to the entire class. If it were inside this function, it would not be available outside, but it is higher in order. And you've also seen how to create instances of the classes, manipulate the variables or properties inside, and call the methods. So that's all for this tutorial. This is Brian, thank you for watching. I hope you found this educational and entertaining.