How to Create Getter and Setter Methods in PHP 


               


How to Create Getter and Setter Methods in PHP

php


In this article, we show how to create getter and setter methods in PHP.

Just as the name implies, a getter method is a method that gets or retrieves a value of an object.

And a setter method is a method that sets the value of an object.

Getter and setter methods are used when you want to provide encapsulation for your data.

You make property of the object you are getting or setting either private or protected, providing encapsulation. You then use the getter or setter method to be able to access the private or protected property of the object.

Therefore, there isn't direct access to the data and you could provide more control of how the data is changed and what values it can accept.

When learning about getter and setter methods, it may seem hard to understand why you would need to provide this type of encapsulation to the data.

So let's take an example how why you would want to provide encapsulation to data?

Let's say you have a class named kids whose objects represents kids. And the property you want to store is the height of each kid.

Without encapsulation, for instance, you can set the height to a value that it can't possibly. For example, you can easily set the value to 1 inch tall or even a negative number. However, if you encapsulate the property and put in a setter method where you put, for example, of instance that you set the height only if it's above a certain level, then this allows you to filter bad data. So it provides the data from bad entries. So this is the power of encapsulation. You run the setter method and you can make it so that the height only gets set if it's a valid, reasonable number. A child's height can't be negative. A child's height can't be 1 inch tall. This is the power of giving encapsulation. It also allows you to filter bad set values, which you wouldn't be able to do if the data isn't encapsulated.

So below is an example PHP code that creates a getter and setter method for kids' heights.



So in the above code we create a class named kids.

Because we want to provide encapsulation to the data, we make the object properties private. In this code, we simply create one property $height. This, of course, is made private or protected to provide encapsulation.

We make the getter and setter methods public, however. We make to provide access to the data. However, within the getter and setter methods, we create standards of what should be done with the data, so that we can decide what is acceptable and what is not.

The rule is to make the data private and the getter and setter methods public. Not that this is always the case, but you can do this for now.

So we now create the setter method. I name it setHeight($height). It accepts the $height parameter. It will set this height to the object you call it on as long as the height is greater than 30 (30 inches).

We then create the getter method. I name it getHeight(). It accepts no parameters. It retrieves an already set value. We echo out the statement that the child's height is however inches tall.

To test the setter and getter methods, we instantiate an object of the kids class, $kid1.

We then call the setHeight() function on the $kid1 object. We pass in the parameter 40. This statement sets the child's height to 40, since 40 is greater than 30. If it was less, then the value wouldn't get set.

We then call the getHeight() function. This echos out the child's height.

So now you've gotten a firsthand experience of what a getter and setter is, what it's used for, and how to create and call them in PHP code.

The encapsulation it provides really helps to filter bad data and gives you, the coder cleaner, safer code that doesn't present bad data.

Running the above PHP yields the following output shown below.

Actual PHP Output


The child's height is 40 inches tall


There's also a way in PHP that create getter and setter methods using the magic __get and __set methods. This is an alternative and very effective way of setting and getting values for a given class.


Related Resources

How to Create a Final Method in PHP




HTML Comment Box is loading comments...