Join Kevin Skoglund for an in-depth discussion in this video Class properties, part of PHP: Object-Oriented Programming.
- [Instructor] In this movie, we're going to learn how to add properties to our class definitions. Properties are simply variables to hold object values. These are also frequently referred to as attributes, class variables, or instance variables, and you'll hear these terms used interchangeably all the time, they mean the same thing. They're even used interchangeably inside the PHP documentation, so don't be confused if you see it called properties one place, and attributes somewhere else. The way that we define properties is with the var keyword, followed by the variable name that we want to use, and our variable name follows all the normal variable naming rules.
We also have the ability to set an initial default value for each property. Let's see an example. Here I have a class Person, and inside there are four properties being defined, first name, last name, employed, and country. You can see that all of them begin with that var keyword, to let it know that it's defining a property, followed by the variable name with a dollar sign in front of it. For the first two, there's just simply a semicolon at the end of the line. For employed and country, there's also a default value that's being set, so it's being initialized to employed equals false, and country equals the string None.
The way that we set values to these attributes, and read them back, is by first creating an instance of the object, and then using arrow notation to reference the property. So here, I have the customer variable at the beginning, followed by the arrow notation, that's the minus sign and then the greater than sign, together they make an arrow, followed by the property name, in this case, first_name. Notice that the property name does not have a dollar sign in front of it. This frequently trips up beginners.
The dollar sign is only in front of the variable itself, customer. The arrow notation takes the place of the dollar sign, we don't need to have it. It knows that we're talking about a property, and it knows that that property is a variable, so there's no reason to put the dollar sign in front of it again. After that, you can see I'm using an equals sign to set a value to that property, so now, this instance of person is going to have a first name property set to Anna. I can also set the last name equal to Martin, then I can read those values back also by using that arrow notation.
Here, I'm asking to echo customer, and then the arrow notation, first_name, and of course it returns back Anna. So, whether we have an equals sign after it or not determines whether we're setting or reading a value, just like with a normal variable. We can also call for the customer country, and we'll get back that default value that we set, the value None, or we can of course set it to a new value, and when we read it back, it will return the new value. Properties is where object-oriented programming gets its power, because now, each of our instances that we create can be different and unique.
We can have a thousand instances of the person object, and every single one of them can have a different first name and a last name, and therefore behave differently because of it. There are a couple of functions that are useful for working with properties. The first is get_class_vars, and it's going to return a list of the properties that are defined in this class. We don't have to have an instance to do it, we just call the name of the class using a string. We also have get_object_vars, which does the same thing, but as an argument, it takes an instance of the object.
They're very similar, they return basically the same information, except that class_vars is going to have the default values for the class, where as object_vars will return the current values for that instance, and then last, we have property_exists, which returns true or false, based on whether a property exists on the class or on the object. You'll notice that the first argument there is called mixed, that's because mixed can either be a class name as a string, or an object instance, it'll accept either one, and then the second argument is a string, which is the name of the property we want to check for.
Let's try these in our oop_sandbox. Let's try these by making a duplicate of our class_instances.php file. I'm going to do that by holding down Control, and clicking on the file name, and choosing Duplicate, and then it'll give me the opportunity to rename it, I'm going to rename this file as properties. It's also fine if you want to create a new file, and just copy and paste that in there, and I'm going to take this code out of here, but I'm going to leave the basic class definition and the instantiation here, and now inside the class definition, let's put some properties.
I'll put var, dollar sign, first_name, and var, space, dollar sign, last_name, and then let's add one more with the default value, country equals None. You can add more if you want, but I think that's adequate, we have three properties now on Student. So, each one of these instances now has that property, right, it's just like ripping off the page of the memo pad. We've created a new student, we have those three properties available to us, so let's try setting one of them.
Let's say Student1, we'll use our arrow notation, first_name with no dollar sign in front of it, it's not necessary when we're talking about a property. I don't want to read back the value, I know there's no value there yet, so let's set a value, let's set it equal to Lucy, and then let's do the same thing, Student1, again using the arrow notation, and then last name, and let's make her last name Ricardo, and then I'm going to copy those two, I'm going to come down here below Student2, I'm just going to change the one to be a two, let's change this one to be Ethel, and the last name to be Mertz.
Alright, now I have two instances of the object, and they're different, they're different because the properties have different values in them. So, the same class, the same object, but a different instance of that object, with different properties, and then let's read those values back, let's try echo, dollar sign, student1, use that arrow notation again, first name, let's put a space, and then we'll do the same thing, student1, and notation, last name, then at the end let's just put a B-R tag, just so it'll skip to a new line in my browser, We'll do the same thing, copy and paste it, with student2.
Alright, so we should get both those students' names back. Alright, let's try out some of those useful functions that we were talking about. How we have one for getting the class_vars, that's going to be get_class_vars, and it's going to take the class name as a string as its argument. Let's look at that with class_vars, I'll put a B-R tag here, we're going to go to a new line, because we want to use P-R-E tags, followed by print_r, because what this is going to return is an associative array, so print_r is going to let us really inspect that array in a nice, pretty format, I'll just copy this line, and make this ending HTML tag, PRE tags will help preserve the formatting that print_r puts out, so we can see that nicely, and then let's do the same thing, I'm going to copy this, we also have another one, instead of get_class_vars, it's get_object_vars, and instead of taking the class name, it takes an instance.
So, let's use student1, that's an instance, and let's just rename this to object_vars, and object_vars, and object_vars, there we go. Alright, so, we're using both of those, and then, last of all, let's use this property exists. So it's a boolean, so if property exists, and inside there, the arguments we're going to pass in, we can pass in mixed for the first one, but let's use the name of the class, and does it have the property first_name? Both of those are strings, and then it'll return two values back, either it will have that property, or it won't.
If it does, let's echo, property, first_name exists in Student class, and then let's just copy that line, and put it here, and change exists to does not exist in Student class. Alright, now we've got all those, let's try them out. Come back over here to Firefox, and instead of class_instances, we're going to be going to class_properties.php.
Here, you can see we're setting the value, and reading back the value for student1 to be Lucy Ricardo, the second one you can see is Ethel Mertz, then you can see those class vars. Now, remember, this is using just the default class, right, so we're seeing the default values, first name and last name have no default values, country has a default value of None. You can, of course, use the keys from that associative array, if we just want to know what the property names are, or we could use the values from it if we just want to know what values are set. Here, when we're working an instance, you can see that I actually see the values here, first name is Lucy, last name is Ricardo, country is still set to None, and last of all, you can see that property exists is returning true, because the message we're getting back is that it does exist.
So, the most important things to remember about properties are that you define them inside your class using var, followed by a variable name, and then when you reference them, either to read a value or set a value, you use the arrow notation without the dollar sign, and last, remember that properties are what make each of our instances unique.
- 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