PHP: Getters and Setters.

In this guide, we are going to show you how to create “Getters” and “Setters” using PHP.

“Getters” and “Setters” are class functions that allow you to control access to an object’s properties.

Sometimes, people will refer to them as “mutator methods”.

To sum it up:

  • A “getter” allows you to retrieve or get a given property.
  • A “setter” allows you to set the value of a given property.

Take a look at the following PHP class:

<?php

class Person{
    
    //The name of the person.
    private $name;
    
    //The person's date of birth.
    private $dateOfBirth;
    
    //Set the person's name.
    public function setName($name){
        $this->name = $name;
    }
    
    //Set the person's date of birth.
    public function setDateOfBirth($dateOfBirth){
        $this->dateOfBirth = $dateOfBirth;
    }
    
    //Get the person's name.
    public function getName(){
        return $this->name;
    }
    
    //Get the person's date of birth.
    public function getDateOfBirth(){
        return $this->dateOfBirth;
    }
    
}

In our Person class, we have two private properties called $name and $dateOfBirth.

Because they are private properties, you will be unable to access them like so:

$person = new Person();
echo $person->name;

If you attempt to run the code above, you will receive the following fatal error:

Fatal error: Cannot access private property Person::$name

To provide access to our private properties, we have created two “getters” called getName and getDateOfBirth.

If you take a look at the Person class above, you’ll see that these functions simply return the values of our two private properties. That is all they do.

Because the visibility of these two properties is set to private, you will also be unable to modify or “set” their values.

Instead, you will have to use one of the “setter” functions that we created: setName or setDateOfBirth.

An example of our getter and setter functions in use:

//Instantiate the Person object.
$person = new Person();

//Set the name to "Wayne"
$person->setName('Wayne');

//Get the person's name.
$name = $person->getName();

//Print it out
echo $name;

In the PHP code above:

  1. We created our Person object.
  2. After that, we “set” the $name property to “Wayne” using our setter method setName.
  3. Finally, we retrieved the value of the $name property by using our getter function getName.

As you can see, this is pretty simple stuff.

Why use getters and setters? What is the point?

The getters and setters that we created in our Person class are pointless.

They merely provide access to our private properties. Beyond that, they don’t really do anything.

However, what if we want to add a layer of validation to stop developers from misusing our object? For example, what if we want to make sure that the person’s $name variable is a string variable and not something else?

Well, we can simply add that layer of validation to our setter method:

//Set the person's name.
public function setName($name){
   if(!is_string($name)){
       throw new Exception('$name must be a string!');
   }
   $this->name = $name;
}

In the PHP code above, we modified the setter method setName so that it validates the $name variable.

Now, if a coder attempts to set the $name variable to an array or a boolean, our function will throw an Exception.

If we wanted to, we could also make sure that the $name variable is not a blank string.