Join Kevin Skoglund for an in-depth discussion in this video Solution: Properties and methods, part of PHP: Object-Oriented Programming.
- [Narrator] I hope that you were able to complete our first challenge assignment. If not, follow along with me as I show you the solution that I came up with. So the first thing that I did was I created a new file inside our OOP sandbox directory called challenge underscore O one dot php. I opened up some php tags, and I started defining a class. The class was going to be for a bicycle object, so we're going to have instances of different bicycles, so it's going to be class bicycle singular with a capital B.
That's the way we want to name our classes, singular nouns with capital letters. If we have more than one word together, we do camel case. Then I've got my curly braces, and then I've got a list of the different properties that I want to declare, brand, model, year, description, and weight underscore KG, short for kilograms. Each one of those is just simply a variable with var as a key word in front of it. For description and weight, I went ahead and added some fault values as well. That wasn't part of the challenge assignment, but I wanted to go ahead and just do that here as well.
Once I had the different properties, then I moved on to the methods. I wanted to have one method called name, so we define that just as we would a normal function. We call function, we provide the name of the function, and then we have parenthesis followed by curly braces, and inside there, we have a value that's being returned, and I'm concatenating together a number of different strings. The strings that you use, the order that you use, the strings that you use around it, is all up to you. You can decide what that would be. For me, I chose brand, space, model, space, and then in parenthesis, the year.
Now for each one of those properties, notice that I'm calling it by using this and the arrow notation. We must do that in order to refer to these properties inside each instance. Then, for weight underscore, lbs, short for pounds, I wanted to be able to convert kilograms into pounds. I'm going to do that by multiplying it by this factor here, the two point two number. I'm going to just take the additional step of making sure that the value that I'm multiplying is a float value, right? It can be an integer or it can be a decimal, but we don't want to accidentally try and multiply a string or something like that by that number, so I'm just going to make sure that it is a float.
Now the default value is a float, but there's no guarantee that this wouldn't get set to something else. So it's just smart programming. There was one more method that was in the challenge assignment which was set weight in pounds, and we'll come back and do that together in just a moment. First, I want to try these basic parts out, and then we'll come back. So, I want to create a new instance, so I use the new key word with the name of the class, so new bicycle, and I assign that to the variable trek. Now I can use trek to refer to that instance, so when I want to talk about the attribute on that instance for brand, then I can use that arrow notation, and I can set it equal to this string, and I did the same thing for the other properties.
You can fill that out with any information you'd like. It can even be a made up bicycle. Next, I created another instance, and I assigned that to the variable CD, short for Cannondale, because this is a Cannondale bike, and I again gave properties values for brand, model, year, and weight. I didn't bother with description, but you could do that as well. Then I wanted to test out those methods, so I'm going to use name. It should return back the name to me. I'm going to echo that to the browser, so I should be able to see it. I did the same thing for the second instance, the Cannondale bike, and then I've got the weight.
So I'm going to output the weight in kilograms, and then weight in pounds. Now notice that one of them is a property, and one of them is a method, and how can we tell that? Because this one has parenthesis after it. That is necessary when we're calling a method. We're talking about a property, we don't have the parenthesis there. Alright, so let's try this out. Let's save it. Let's go over here, and instead of class methods, let's load up challenge underscore O one dot php, and there you go.
You can see the name of each of those bikes, so we know that we set those properties correctly. We were able to use our method to retrieve them and put them together into strings, and you see that we get back the weight in kilograms, and the weight in pounds. Now let's do that last one together. So we have one more function to write, and that is set, underscore, weight, lbs, and of course, it's going to need an argument. So I'm just going to call that value, so an argument is going to be passed in. We're going to have to set it like that.
Now I can't just use weight underscore lbs with a value, because that name is already taken by this method. That's why I've appended set underscore in front of it, and in order to set that, we know what we need to do. We want to be setting this weight in kilograms equal to, and then we're going to take the float value of value, whatever's passed in, and we're going to divide it by this number here, two point.
There we go. Okay, so that's going to convert whatever value we've been given in pounds into kilograms and then stored in that attribute. So we can store values now two different ways. We can set them here by setting the property on the instance directly using the equal sign, or we also have the ability now to say trek, and let's use set, weight, lbs, and then let's put in just a nice round number two.
So that's going to set it equal to two. Now let's just copy these two lines, come down here, and we'll just output those values again. We're going to output it in kilograms and in pounds to see what it equals. So we're able to set the value of the weight in two different ways now. Let's try this out. Go back over here. We'll reload the page, and there you go. You see the weight in kilograms, and we convert back to pounds, it's two, which is what we expect, 'cause that's what we put in to start with right here.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project