Risc OS Posted June 2, 2010 Share Posted June 2, 2010 Our getters / Setters neccessary? I'm new to OOP and want to know if they are something that must be used. Some classes have 12 or more properties, that's 24 extra classes just to change/get a property value. Are getters/setters just for private properties? Or do I have to use them with public ones too... Quote Link to comment https://forums.phpfreaks.com/topic/203617-getters-setters/ Share on other sites More sharing options...
trq Posted June 2, 2010 Share Posted June 2, 2010 Its best practice not to have any public properties and have getters/setters for everything you need access to from outside of a class. PHP provides overloading which can make the process simpler. Quote Link to comment https://forums.phpfreaks.com/topic/203617-getters-setters/#findComment-1066570 Share on other sites More sharing options...
Risc OS Posted June 2, 2010 Author Share Posted June 2, 2010 Ok, but what about setting getting from within the class? Must I use them then too? or are they just for outside access? Quote Link to comment https://forums.phpfreaks.com/topic/203617-getters-setters/#findComment-1066572 Share on other sites More sharing options...
Mchl Posted June 2, 2010 Share Posted June 2, 2010 You can access these variables drectly (from within object context), as long as you trust yourself not to mess anything up. (A setter can have some validation logic built in, which you will bypass when accessing a field directly). Quote Link to comment https://forums.phpfreaks.com/topic/203617-getters-setters/#findComment-1066575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.