How to Use the PHP __get and __set Magic Methods 


               


How to Use the PHP __get and __set Magic Methods

PHP


In this article, we show how to use the PHP __get and __set magic methods.

The PHP __get and __set magic methods function as getters and setters for object values, but it had the added advantage in that you don't have to declare the object properties (variables) in the class.

Instead when you set a value (with the object property never declared in the class), the __set magic method is automatically called and sets the value.

The same is true for the __get method. Without the object property being declared in the class that you want to get, the __get magic method is automatically called and gets the value.

So the __get and __set magic methods function as getters and setters, without the object property variable having to be declared in the class.

We will show each of these in the code below.

In the code below, we make a class called kids. In this class, we set a child's height through the __set method or get a child's height through the __get method.

Getter and setter methods provide encapsulation of data so that in order to set or get data, it has to pass through a method. The reason we do this is because many times we want to test the data first to make sure that's an appropriate or reasonable value.

In this class, we are setting the height of a kid. For example, the height cannot be negative. The height can't be 1 inch tall. No child is one inch tall. This is the reason we only allow for heights 30 inches or grader. Imagine this is fifth grade student. They all are way past this point.

So passing data first through a method allows us to filter out bad data. It allows for this encapsulation where we don't have to accept bad data, such as a negative height or 0.5 inches tall.

This is why getter and setter methods are commonly used.



So we create a class called kids.

Inside of this class we create the setter and getter methods.

The first method that we create the setter method using the __set function. The setter function has to accept 2 parameters, the object property you are defining and the value of this object property. As we are setting a child's height in this code, the height is the object property and the value of the height we set it to is the value. We make it so that if the value passed into the object property is greater than 30, we set the object property to that value. If the value is less than 30, then the magic set method does not set the value.

We then create the next function, the getter method using the __get function. The get function accepts one parameter, the $property of the object. We do not need the $value as the parameter, because the $value is already set by the setter function. With the getter function, we do not set the value. We just retrieve the value that's already been set. So in this getter function, we return the child's height in inches by calling the $this->property function. $this->property refers to the value of the property of the object we're getting.

All of this will make more sense now below.

So underneath this, we create an object of the kids class, $kid1.

We then the object property, height, of $kid1 equal to 45. This makes the kid's height 45 inches.

What'a amazing about the magic method __set() in PHP is that we have defined the property variable $height anywhere in the class. Yet we're just automatically setting the height of the $kid1 object. This triggers the PHP __set() magic method. The method sees the height as a property of the object and 45 as the value of this height property. $kid1 is the object of this call.

So because we automatically set an object property and its height without declaring them or calling a function, this automatically calls the PHP __set() magic method. So the $property value is height. The $value of this property is 45. We set the value only if the value is greater than 30. We set the value through the line, $this->property= $value. $this refers to the object kid1. We set $kid->height= 45. So now you can see how this is set through the PHP __set magic method.

This covers the set part.

To automatically get the property of an object, you call an output function such as echo or print with the parameter of an object and property. This automatically triggers the PHP __get magic method.

The only parameter needed for the __get() method is the $property variable. We return out what the child's height is in inches.

So this is all that is required to use the PHP __get and __set magic methods. It's very adaptable because you don't need to declare variables in the class and you don't need to call a function. If you set it up properly, like how demonstrated, it will automatically call the __get or __set magic methods. So in this way, it simplifies code.

Running the PHP code above yields the following output shown below.

Actual PHP Output


The child's height is 45 inches tall


Related Resources

How to Create Getter and Setter Methods in PHP



HTML Comment Box is loading comments...