From: A B. <ab...@ab...> - 2001-05-13 21:14:36
|
In light of the recent discussions about variable and method naming conventions I'd like to add my suggestions based on what I've used in numerous large projects in C++, Java, and lately in PHP that tend to feel quite natural for at least a few pockets of programmers laboring in unlabeled buildings in a few urban centers: 1.) Variable and method names: mixed case with lower case leading, with leading or trailing underscores to signify private or global. e.g. $userName seems to be favored and $_userName for a local and "private" variables, $userName_ for global and for "public". Digression on associating private/local with leading underscore - we read code from left to right and somehow the underscore being located to the left seems to connote something that is more restricted or internal. Whereas a trailing underscore seems to imply something "further out" or global or public. At least for many programmers I've worked with. This seems to agree with the http://www.thyrell.de/info/downloads/CodeGuide.pdf document. Sadly, function names are not case sensitive in php, so one has to be a bit careful. 2.) Getters and setters: I agree with Aderhold re: getProperty() and setProperty(...) seem quite natural. It's too bad that php doesn't support function overloading, it would be nice to be able to define setter and getter functions as: $obj->name_() - gets name value, name($stringVarHere) - sets name value $obj->name_($someName_); sets the $_name property of $obj to equal $someName_ It would be nice if binary cloud allowed this convention also. One simple workaround to the overloading problem: class test { var $_name="not set"; function name_($val) { if(isset($val)) $this->_name=$val; else return $this->_name; } } /* exercise class test */ $obj=new test; $tvar=$obj->name_(null); echo "obj name is $tvar<br>"; $obj->name_("set"); $tvar=$obj->name_(null); echo "obj name is $tvar<br>"; The above are just my own thoughts - I wanted to add my vote to the discussion. Thank you. |