Differences between Getters and Setters
Getters |
Setters |
---|---|
Used to retrieve the value of a property. |
Used to modify the value of a property. |
Typically named with a “get” prefix (e.g., getName). |
Typically named with a “set” prefix (e.g., setName). |
Do not take parameters (or take optional parameters). |
Take parameters representing the new value to be set. |
Used for reading the state of an object. |
Used for modifying the state of an object. |
What are getters and setters methods in PHP ?
In object-oriented programming, getters and setters are methods used to access and modify the private or protected properties of a class. These methods provide a controlled way to retrieve and update the values of class properties, promoting encapsulation and maintaining the integrity of an object’s state.