What are Getters and Setters?

getters and setters

Getters and Setters seem like a really long winded way of dealing with the values assigned to properties in objects. Most likely, you’ve been used to simply assigning values to your variables within your procedural code without issue. So why get all fancy in object oriented PHP and start to deal with more complicated ways of assigning and accessing the data that properties in our objects hold? What’s the point? Well, there are good reasons for this, and we will try to understand them in this tutorial.

Let’s start with creating a class using our hunt down the nouns approach. How about a Motorcycle, that sounds fun right? Sure.

Assuming we have a motorcycle, this motorcycle will have come from a manufacturer. In fact, every single motorcycle has a manufacturer so let’s set up a way to make sure our motorcycle objects have a manufacturer from the get go. We can use our handy magical constructor function to allow for that.

This code above here represents the day you convinced your wife to allow you to purchase a brand new Harley Davidson. Cool. Now being the tinkerer that you are, you figure you want to be able to customize your new Harley. Maybe you have an obsession with tires, and you want the ability to easily change them based on weather conditions. Sounds great, let’s add that ability now.

Awesome! That was pretty easy. What if you decide you want to change out your Dunlop tires for a set of Pirelli tires because you heard they are better in the summer. That’s easy to update.

Now you may be thinking, “We’re both assigning and retrieving values from our property, and we haven’t even talked about these silly getters and setters. What gives?” That’s a valid thought actually. There is a good reason for them though, and we’ll explore it now.

Increase Security With Getters and Setters

Let’s imagine you go to a race with your new Harley. Competition is fierce and the racers want every edge they can find to win. Out of the many racers present, the slowest qualifier was running Cooper tires. You on the other hand were the fastest qualifier on a fresh set of Dunlops. RacerX decides he is going to sabotage your efforts and swap his Coopers onto your Harley, while stealing your Dunlops and putting them on his Bultaco. There is nothing to protect you from that. Let’s see.

Boom. Just like that, you go from hero to zero on the track. We need a way to prevent RacerX from tampering with your machine again.

Introducing Getters and Setters

We can do just that by implementing a special method called a setter, and RacerX is going to try and abuse that setter but we know better.

At this point, RacerX is going to try and use our new method setTires to maliciously install a set of poorly performing Cooper tires on our race ready Harley. Take note however of the logic we placed inside our method. Before we assign the tires property, we do a quick check on it’s value. We set up a rule such that if the tires are Cooper, we will throw an exception and not allow those tires to be installed. It turns out, this is the only way we can implement this type of security or behavior to the act of setting a value to our property. When we work with properties directly, there is no way to hook in to this behavior, which is why getters and setters are a good thing.

Since we have shown how to use a setter, let’s also review how to use a getter. It’s pretty easy, we simply do it like so:

We have not yet added any behavior to our getter method. It would seem that there would be no reason to since all we are doing is returning a value, but if you think about it, maybe you want to apply some type of formatting to your data. In this case, let’s imagine that the sanctioning agency wants to check the tires of all racers, and they only accept the brand of tire in all uppercase letters. We can implement that with our getter like so.

We simply wrap our return statement with a call to strtoupper and we are in business.

Note: It is a common convention to use camel case for getter and setter methods. That means the get or set portion of the method name is lowercase, followed by the property we are getting or setting with the first letter capitalized. By using this approach, consumers of our class are forced to abide by the logic we set in our hooks. In addition to this approach, PHP also has a built in way of dealing with getters and setters via the magical method special functions __get() and __set().

The Problem With Public Properties

One thing we didn’t mention yet, is that our tires property is still public. We created some pretty slick data checking and integrity on our getters and setters, but guess what, if our malicious competitor wants to continue abusing our tires property via syntax like $motorcycle->tires = 'Cooper' instead of $motorcycle->setTires('Cooper'), he could do that. The reason he can do that is because our tires property is still public. We need a way to force users of our class to only use our getters and setters, and prevent them from trying to muck with things directly. We can do this with encapsulation which we’ll talk more about soon.

Getters and Setters Summary

Getters and Setters play an important role in object oriented programming. They allow for us to implement behaviors and hooks into what values are allowed to be stored in the properties of our objects. This provides a nice way to increase security, and prevent malicious tampering of our code.