Page 1 of 2
PHP5 practices
Posted: Mon Jul 02, 2007 9:19 pm
by alex.barylski
Do you:
1) Use the public access modifier on functions or leave as is assuming the default public role? In C++ you assign sections as private, public, etc (defaulting to I believe public). In PHP5 (I assume anyways) everything is defaulted to public. So I ask, do you bother being explicit and using the modifier to avoid any confusion with lesser experienced developers or not?
2) Store member variables at the beginning of your class or at the bottom, out of sight and out of mind (I've recently switched from the latter to former).
If you can give practical reasons to either or (experiences, etc) to backup your preferences, even better.
I thought this was general enough to remain in this forum as opposed to T & D.
Posted: Mon Jul 02, 2007 9:25 pm
by John Cartwright
1) I always declare it public if it is in fact supposed to be public, simply to keep things consistent and to avoid any confusion.
2) I keep my member variables at the beginning of the class so it is clear what variables the class is working with, along with their scope and comment blocks.
Posted: Mon Jul 02, 2007 10:32 pm
by alex.barylski
Jcart wrote:1) I always declare it public if it is in fact supposed to be public, simply to keep things consistent and to avoid any confusion.
2) I keep my member variables at the beginning of the class so it is clear what variables the class is working with, along with their scope and comment blocks.
Perhaps it's just because I need change constantly...I've just moved all my members to the top of the class and already feel better about it
It's been a while since I've looked at some of this code so it's nice to know immediately (without having to scroll to the bottom or CTRL+END) what variables are defined. I've tried explicitly stating functions as public, but it messes with my aesthetic layout (nothing more) so I've opted to drop making it obvious and just to go with defaults.
Thanks for the feedback. I find that most helpful about community, when your sitting on the fence with a subject and cannot make up your own mind, someone will surely sway you in one way or another.
Cheers

Posted: Mon Jul 02, 2007 11:36 pm
by feyd
I declare members at the beginning of the class and all members and methods have access modifiers, public or not.
Posted: Tue Jul 03, 2007 12:02 am
by alex.barylski
feyd wrote:I declare members at the beginning of the class and all members and methods have access modifiers, public or not.
heh...I'm missing something...PHP5 defaults to private on member variables? If you don't specify access for members, you allow them to directly edited?
Posted: Tue Jul 03, 2007 12:05 am
by feyd
Hockey wrote:PHP5 defaults to private on member variables?
I didn't say or imply that.
Hockey wrote:If you don't specify access for members, you allow them to directly edited?
All members are protected or private in my book. I provide accessors and/or mutators to manipulate them unless I don't care what they contain, in which case they would be public. .. however it's very rare I would not care what they contain.

Posted: Tue Jul 03, 2007 12:08 am
by alex.barylski
feyd wrote:Hockey wrote:PHP5 defaults to private on member variables?
I didn't say or imply that.
Hockey wrote:If you don't specify access for members, you allow them to directly edited?
All members are protected or private in my book. I provide accessors and/or mutators to manipulate them unless I don't care what they contain, in which case they would be public. .. however it's very rare I would not care what they contain.

A second read revealed I read that that wrong. My bad. It sounded as though you said you declare all variables at the top of the class and methods have access modifiers. When reading it like that, it sounded as though you didn't indicate the access level of your variables. Again my bad, sorry.
Posted: Tue Jul 03, 2007 12:28 am
by feyd
Hockey wrote:A second read revealed I read that that wrong. My bad. It sounded as though you said you declare all variables at the top of the class and methods have access modifiers. When reading it like that, it sounded as though you didn't indicate the access level of your variables. Again my bad, sorry.
No worries, it happens to all of us once in a while.
Posted: Tue Jul 03, 2007 2:31 am
by Maugrim_The_Reaper
Same as feyd - I declare all variables as either private or protected (usually protected to enable easier subclassing unless the field really should never be changed by a subclass). Using public properties I reserve for those times when the object is a data container and it makes the API that bit less complex and baggy. Even here I sometimes use __set() and __get() so I can enforce pre/post processing on variables if ever needed later on.
I always use the visibility keyword - it's a form of documentation even if covered by defaults. I prefer to make it as obvious as possible.
Posted: Tue Jul 03, 2007 6:23 am
by superdezign
I try to strive more for private than protected. I feel protected is used for objects to be inherited, but member variables stay private so, even through inheritance, they must invoke the get/set methods.
Posted: Tue Jul 03, 2007 11:05 am
by RobertGonzalez
+1 feyd and Maugrim, except I always start with private for all functions and loosen them as needed throughout the app, or, as feyd pointed, create accessors to interact with them. I also declare all of my class properties up top, just for consistency with legacy code and typical application layout.
Posted: Wed Jul 04, 2007 6:26 am
by Maugrim_The_Reaper
I headed the opposite way some time ago - protected by default, unless a field really needs to be private. From my perspective, a protected property is not accessible in the public API, so it's a good default unless subclassing is not going to be a concern, or a subclass can use a mutator/accessor pair instead (no need for them to have the property access). And even then I find myself leaning towards more public entrails so runtime configuration is easier (assuming that level of access is actually useful, and not just for inter-class manipulations which can end up just becoming an additional layer of coupling).
Granted, having folk keep pounding an issue tracker over subclassing problems likely colours my view

. It's easier to grant them subclass access more openly than waiting for the reported issues and improvement requests to start mounting up.
Posted: Wed Jul 04, 2007 6:41 am
by Chris Corbyn
I used to use private all the time too. I haven't done for over a year now because it just runs you into problems when you decide to subclass. Usually the access modifiers are there to indicate which bits of a class or part of the API, and which are internal. If it's internal then more often than not a subclass may make some use from it.
Typically I make all properties protected and provide accessors for them as part of the public API. I very very rarely make properties public directly since it's too easy to screw up (or should I say, too easy for other's to screw up). Class constants come in handy too.
Posted: Wed Jul 04, 2007 6:55 am
by feyd
I only set properties to protected if the mutator would do nothing outside of setting the value. If any logic needs to be done, which can often be the case in PHP, it's private with a protected (possibly public) mutator.
Posted: Wed Jul 04, 2007 11:48 am
by alex.barylski
superdezign wrote:I try to strive more for private than protected. I feel protected is used for objects to be inherited, but member variables stay private so, even through inheritance, they must invoke the get/set methods.
I am of the same mindset.
