I have been coding in php and using codeigniter as well, never got the concept of getter and setter. What does it mean ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The getter and setter here are the methods that allow access to the protected property
$_bar. The idea is not to directly allow access to the property, but to control access to it through an API for your client code to consume. This way you can change the underlying state, while leaving the public facing methods as is. Thus, you are less likely to break a client if changes occur.Another reason to have them, is when you need to add logic before you get or set a property. For instance, a setter might validate and uppercase the value
or a getter may lazy instantiate some object
Some people criticize getter and setters as boilerplate code, especially if they do not do anything than directly setting/getting the property they provide access to. That discussion is beyond scope though. Read more about it at