- [Instructor] In this movie we're going to talk about another concept in PHP called setter and getter methods. This folds right in with what we've been talking about about visibility. Let's start by using a metaphor. Let's say that we have a bank account and we want to put money into our bank account. So we have the ability to do that. We can put money in the bank and we can take money back out of the bank. Now, in reality, that's not the way it works. We can't just walk into the bank vault at our local branch and put money in and take money out.
Instead, what we have is a teller. The teller's there, you walk up to the desk, you tell the teller you'd like to deposit money, the teller brings up all your account information, they authorize you, and then they take care of putting the money into your bank account. And if you want to get money out, again you talk to the teller, the teller authorizes you, makes sure that you have enough money in your account, that there's no problems, make sure the bank has enough money to pay you, and then they take that money out, they give you your money, and you go your way.
The teller acts as an intermediary for both putting things into the bank and bringing them back out again. That's how setter and getter methods work. The idea is that you would set the property visibility to be private so it's not accessible directly. That would be like our bank account. We can't access it directly; it's private. But, then we can define a method which does have access to that private property and it can set the property's value for us and we can define another method that will get the property's value bank out.
See those words, set and get? That's where we get the names, setter and getter methods. It's a very common design pattern. Here's an example using a class called product. I've made it's name private. So now, when I create a new instance, I'm not able to set a value or read a value back from the name property; however, notice that there's also two public functions, one called set_name and one called get_name. And guess what they do? They set those values for me.
The first one just simply sets the value's name, the second one just returns the value back to me. So setter and getter methods allow access to what would otherwise be private properties. This is useful because it allows us to regulate access the same way that the teller at the bank is able to regulate our access to the bank account. It's also useful if we have situations where we only want to be able to read properties back or only write those properties. Setter and getter methods are also useful if we want to pre-process values.
That is, we want to take some information, we want to do some processing on it before we put it into our property, or we want to pull the value out of our property and do some processing on it before we return it back to the user. What you want to avoid however, are what are referred to as naive setter and naive getter methods. The example that we just looked at with product and name is an example of a naive setter and getter. This is just simply less efficient and slower than giving us access to the property as a public property.
We have zero gain to our code, there's zero benefit for security or anything else like that, it's just simply slower. So don't do this version; this is the simplified version and it's what you should not do. Let's look a a better example. Let's say that our product class has another private property called price. I'm going to have two public functions, set price and get price that can interact with that private property. The first one, set price, is going to take the value and it's not going to assume that it's an integer or a float value, instead it's going to strip out any dollar signs or commas that might be in the value, in case we were given something that was a string, and then it's going to turn it into a floating point number, and it's going to check and see whether or not it's less than or equal to zero, and if it is, it's going to come back and return an error saying this product or price can't be set to a negative value.
Then, once all of that pre-processing is done, it'll actually set the value of the property. And then, when we get the price back, I'm not simply just going to return the price, I'm going to put that dollar sign in front of it, I'm going to convert it to a string and make sure it has two decimal places. The way you decide to utilize setter and getter methods inside your own projects is going to depend on what you need to accomplish in each project. It's important just to understand the concept. The fundamental idea is that the property remains private but we still have access to it through public methods.
- 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