stuffradio Posted January 9, 2008 Share Posted January 9, 2008 Is it best to set variables in a class to public or private. Also, what are the best ways to effectively design a class in PHP5. Link to comment https://forums.phpfreaks.com/topic/85264-best-designing-class-practices/ Share on other sites More sharing options...
Liquid Fire Posted January 10, 2008 Share Posted January 10, 2008 is it pretty much standard(as far as i know) that members are remain private and you provide access methods(class functions) that can change them. The reason for this is that you can control what can be done to the member so if you only want a number to be one member, you can make sure it will always be a number and so on. Link to comment https://forums.phpfreaks.com/topic/85264-best-designing-class-practices/#findComment-435080 Share on other sites More sharing options...
448191 Posted January 12, 2008 Share Posted January 12, 2008 True. It's a generic OO heuristic that "all properties should be private", but arguably it holds even more true for PHP classes, since PHP is loosely typed. Sometimes protected is more appropriate, but I can't remember the last time I declared a property public. Link to comment https://forums.phpfreaks.com/topic/85264-best-designing-class-practices/#findComment-437084 Share on other sites More sharing options...
br0ken Posted January 12, 2008 Share Posted January 12, 2008 Yeah I've always been taught to have member data as private and the name prefixed with an underscore. eg. private: _testVar; And then to get/set create the following functions in the class function getTestVar(); function setTestVar(var); Link to comment https://forums.phpfreaks.com/topic/85264-best-designing-class-practices/#findComment-437274 Share on other sites More sharing options...
448191 Posted January 12, 2008 Share Posted January 12, 2008 The underscore practice is a relic from the PHP4 era, when PHP knew no property visibility. If everything is either private or protected, there's little use in adding an indicating prefix, is there? Link to comment https://forums.phpfreaks.com/topic/85264-best-designing-class-practices/#findComment-437288 Share on other sites More sharing options...
Liquid Fire Posted January 12, 2008 Share Posted January 12, 2008 I was taught that is C++ to show that the variable was an internal variable and not something being passed, I no longer do that in C++ anymore. There is even less reason to do that is PHP since to access a class member to need to use the $this variable anyways but I guess it comes down to a matter of personal preferences, just like I would write: function get_test_var(); function set_test_var(var); Link to comment https://forums.phpfreaks.com/topic/85264-best-designing-class-practices/#findComment-437291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.