What are getters and setters methods in PHP?

In this article, we learn the best way to create getter and setter methods in PHP. Getter and setter methods are utilized when we need to restrict direct access to variables by end−users. Getters and setters are methods used to define or retrieve the values of variables, normally private ones.

Just as the name suggests, a getter method is a technique that gets or recovers the value of an object. Also, a setter method is a technique that sets the value of an object.

Why Use Private Properties?

Let's first understand why direct access to private properties fails ?

<?php
   class Person{
       private $name;
       public function setName($name){
           $this->name = $name;
       }
       public function getName(){
           return $this->name;
       }
   }
   $person = new Person();
   echo $person->name; // This will cause an error
?>
PHP Fatal error: Uncaught Error: Cannot access private property Person::$name

Proper Implementation

To access private properties correctly, we must use getter and setter methods ?

<?php
   class Person{
       private $name;
       
       public function setName($name){
           $this->name = $name;
       }
       
       public function getName(){
           return 'Welcome ' . $this->name;
       }
   }
   
   $person = new Person();
   $person->setName('Alex');
   $name = $person->getName();
   echo $name;
?>
Welcome Alex

Advanced Example with Validation

Setters can include validation logic to ensure data integrity ?

<?php
   class User {
       private $email;
       private $age;
       
       public function setEmail($email) {
           if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
               $this->email = $email;
           } else {
               throw new Exception("Invalid email format");
           }
       }
       
       public function getEmail() {
           return $this->email;
       }
       
       public function setAge($age) {
           if ($age >= 0 && $age <= 120) {
               $this->age = $age;
           } else {
               throw new Exception("Age must be between 0 and 120");
           }
       }
       
       public function getAge() {
           return $this->age;
       }
   }
   
   $user = new User();
   $user->setEmail('john@example.com');
   $user->setAge(25);
   echo "Email: " . $user->getEmail() . "
"; echo "Age: " . $user->getAge(); ?>
Email: john@example.com
Age: 25

Key Benefits

Getter and setter methods provide several advantages:

  • Encapsulation − Hide internal implementation details
  • Validation − Control what data can be set
  • Data transformation − Format data before returning
  • Access control − Restrict read/write operations

Conclusion

Getter and setter methods are essential for maintaining data encapsulation and validation in PHP classes. They provide controlled access to private properties while allowing you to implement business logic and data validation. Use setters to validate input and getters to format output as needed.

Updated on: 2026-03-15T08:11:59+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements