This is the concept of data hiding (or encapsulation) in OOP. For example, if you want to have a specific property in your class, say โAmountโ and give the client of your class the ability to change or retrieve its value. You must make your Sum variable private (not visible to those using your class) and generate two methods: getter and setter, which manipulate your value (public).
The reason is to be able to check or manipulate the data before setting or getting your value. Here is a quick example:
class test { private $count; //those who use your class are not able to see this property, only the methods above public function setCount( $value ) { //make some validation or manipulation on data here, if needed $this->count = $value; } public function getCount() { return $this->count; } }
source share