Classes, Properties & Methods

Brad Traversy
A free video tutorial from Brad Traversy
Full Stack Web Developer & Instructor at Traversy Media
4.7 instructor rating • 14 courses • 251,531 students

Learn more from the full course

Object Oriented PHP & MVC

Build a custom object oriented PHP MVC framework and then build an application with it

06:39:40 of on-demand video • Updated November 2017

  • Build a custom MVC Framework from absolute scratch
  • Build an application using your custom framework
  • Create a custom PDO class to handle database interactions
  • Complete user authentication using OOP & PDO
  • Deploy your custom PHP application
English [Auto] All right guys so we're going to get started learning about object oriented programming. Like I said this is going to be all about basics and fundamentals. So if you already know this stuff you can feel free to skip it or you can follow along as a refresher. So what I'm going to do here in my server folder which is my HP docs inside my x Sam folder. I'm going to create a new folder for the next few lectures and I'm just going to call this opii sandbox. All right. And we're going to go ahead and enter that and I'm actually going to open this folder with Visual Studio code so you can do that by just right clicking and going to open with code. And that should open it up in the editor. All right. Now if you're not using Visual Studio code just however you want to do it usually you can go to like file open folder or something like that. Just add the folder to your text editor. All right so in the O P sandbox I'm going to create a new file to work with. And we're going to call it to underscore three dot ph P because this is Section 2 and this is lecture 3. We can just close this stuff out. All right. Now we're going to just put in some ph p tags here. All right. And let's just echo out. For now we'll just say echo test just to make sure we can load this. I'm going to save it and we should be able to go over here and go to local host slash o p sandbox slash to underscore three dot P P. OK and we get our test. Echo no. So let's just get rid of that. And now we're going to create a class. All right. Now to create a class we just use the key word class and then whatever we want to call it in this case I'm going to call it user. All right. And I'm just going to put some simple comments in here as well. So this is to define a class. Now as I said before classes can have they can have properties and methods. So properties are basically just like attributes. All right. So for example a user might have a name and email and age things like that. And then we also have methods which are basically just functions. OK so a user might have a register method a log in things like that reset password you know just they're just functions inside of a class. So to set up property what we can do here is we're going to say public and don't worry about the public. Yeah I'll get to that later. And then let's call this name. OK so we'll give this user a name now we can just do that. And that will set up it'll just declare the property. But you can also set it here as well. So for now I'm just going to send it to Brad. All right. So we now have a user class with a name property that has a default of Brad. Now this is enough here to be able to create an object from this class. So let's go down here and let's go ahead and instantiate a user object from the user class. Now to do that we can just create a variable. So I'm going to call this user 1 and we can say equals new. And then the class names so equal new user with some parentheses. And that actually creates a new object. OK if I save that and reload we're not going to see anything over here because we don't have any output. All we did was create an object. Now since this is public we can access the properties directly. All right. And I'm going to show you that's not really the best way to do it I'm going to show you later on how to how to get around that how to set these to private but still be able to access them. But for now we can actually echo user 1 and then we're going to use this arrow this hyphen and then greater then sign and then we can access any property such as name. All right so let's save that let's reload over here and you'll see we get Brad. OK. Now that's a prop.. Let's create a method for our class now. All right so let's create a method called Say Hello k methods will also get an access modifier which can be public private or protected. And again I'll get to that later. But we're going to create a function here called Say hello. OK. And you'll see it's created just like a regular function we have our parentheses and then we open some curly braces and then the function body or the method body goes in here. So what I'm going to do is I'm going to say Ecko let's just to echo Hello for now. All right. Now to for our object that we instantiated down here to be able to use this method we can just say actually know what I'm not going to echo this I'm going to return it and then down here people say Echo say hello. OK I'm sorry. User 1. Say hello and save and reload and we get hello. And just to separate this I'm going to just echo on a line break. All right. Now let's say that we want to access a property from within this method. OK in this say hello method I'm just going to space this out a little bit. All right so let's say we want to access the name so to do that we use a key word called this. So let's say we wanted to say Brad says hello. So we can say money sign this referring to this class or this particular object. And then we can say name and then we need to concatenate because we're using a string here so to concatenate we'll use a dot and we'll say says hello. All right so let's reload or let's save and reload and we get Brad says hello. So any time you want to access a property or another method from within a method you want to use the this keyword. OK. So up to this point we've created one user from our class. Now you can create as many uses as you want. Let's go ahead and create another one. So say create new user and we'll say user 2 they will set the variable user to to new user. All right. And then again we can echo out the name so we'll say Echo user to name and then let's also do to put a line break it's going to get rid of these spaces here. All right. And then let's do this say hello soul say go user to say hello. All right. So let's save and reload and we get the same thing. Actually let me put another line break here. OK. Now we probably don't want to have Brad as the name for every user we create. So there is a much better way to to to do what we're about to do and that's using a constructor. But I don't want to get into that just yet. We're going to get into that in the next lecture. But for now what I'm going to do is just get rid of this so it's not we're not setting a default value we just have a name. Now if I save and reload it's just going to give us just nothing for the name. So down here since this is public since the name is public we can actually set it. So once we create user one we can then say user one name equals and we'll give this one the name of Brad and then down here once we instantiate user 2 we'll say user to name the pools and let's say Jeff. All right so now if I save and reload what I do here I forgot that too. All right so now we get Brad says hello and then Jeff says hello. So we've set different names for both of these user objects. All right. So I think that's going to do it for this video hopefully. This just gives you an idea of how to create a class. What a property is and how to create a property. You can have a default value or not and how to create a method which is just a function inside the class. Also how to access properties from within the class using this and then how to instantiate a user or whatever kind of class or object you have. And then how to use the properties and methods. Once you once you create or instantiate that user. Now as I said this is not really good practice to be able to access these properties directly. Usually you want to only access these through methods. So in the next video we're going to take a step toward that and I'm going to show you what a constructor is OK as well as a destructor. So I will see you in the next lecture.