dpacmittal Posted June 18, 2009 Share Posted June 18, 2009 I read in a tutorial somewhere (actually, 2-3 different tutorials) that its a bad practice to directly access data members (variables inside class) of a class. But they never explained why its a bad practice. Can anyone explain me why its a bad practice? Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted June 19, 2009 Author Share Posted June 19, 2009 bump Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 19, 2009 Share Posted June 19, 2009 I think you're referring to using getter and setter methods as better practice than accessing the data directly. By providing a method with 1 course of action (viewing) or (changing) the data, it is usually prone to less mistakes. It's about controlling the integrity of the data afaik. Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted June 19, 2009 Author Share Posted June 19, 2009 I think you're referring to using getter and setter methods as better practice than accessing the data directly. By providing a method with 1 course of action (viewing) or (changing) the data, it is usually prone to less mistakes. It's about controlling the integrity of the data afaik. Thanks for the info. Quote Link to comment Share on other sites More sharing options...
xangelo Posted June 19, 2009 Share Posted June 19, 2009 A big pro with using getter/setter methods is that it provides a bit of future-proofing. Say a few months down the line you need to make an update to your code and modify what happens to the variable. For example: <?php class Robot{ var $numFingers = 5; } Directly accessing the member would give you the right answer, until you decided that you were adding the arms functions. Now, $numFingers is the number of fingers on each arm. So the total number of fingers would actually be $numFingers*$numArms. If you were directly accessing the data then you would have to apply the multiplication outside the scope of the class. However, if you had a getter/setter method, you could just update your setter method to multiply $numArms*$numFingers and then you wouldn't need to change any code that required those values. I hope the example helped visualize it a little. Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted June 20, 2009 Author Share Posted June 20, 2009 A big pro with using getter/setter methods is that it provides a bit of future-proofing. Say a few months down the line you need to make an update to your code and modify what happens to the variable. For example: <?php class Robot{ var $numFingers = 5; } Directly accessing the member would give you the right answer, until you decided that you were adding the arms functions. Now, $numFingers is the number of fingers on each arm. So the total number of fingers would actually be $numFingers*$numArms. If you were directly accessing the data then you would have to apply the multiplication outside the scope of the class. However, if you had a getter/setter method, you could just update your setter method to multiply $numArms*$numFingers and then you wouldn't need to change any code that required those values. I hope the example helped visualize it a little. Yes, it helped. Thanks Quote Link to comment 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.