A getter method in PHP Object-Oriented Programming is used to safely retrieve the value of a private or protected property from a class.
getInstead of accessing properties directly, PHP encourages the use of methods to control how data is accessed from outside the class.
// Define a class with private property and getter method
name = $name;
}
public function getName() {
return $this->name;
}
}
$obj = new Student();
$obj->setName("Meghraj");
echo $obj->getName();
?>
The getter method getName() returns the value of the private property $name, allowing safe read-only access.
This flow demonstrates encapsulation:
getName()Car class$brandsetBrand() and getBrand()