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. |
From: alex b. <en...@tu...> - 2001-05-14 01:13:38
|
> feel quite natural for at least a few pockets of programmers laboring in > unlabeled buildings in a few urban centers: heh > 1.) Variable and method names: mixed case with lower case leading, with > leading or trailing underscores to signify private or global. This is certainly the standard for Javascript, and indeed many other languages. > e.g. $userName seems to be favored and $_userName for a local and > "private" variables, $userName_ for global and for "public". I've never seen the latter. Though it is interesting, and would still work with the "global variable concept" I presented before. PHP Replaces the . with an _ regardless of where it is in the name. > 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. I agree here. > 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 I don't quite agree there, but at the same time I do like the fact that it solves the "private methods vs. public variables, that's counterintuitive" problem. > 2.) Getters and setters: > > I agree with Aderhold re: getProperty() and setProperty(...) seem quite > natural. as does any other verb-noun combination: checkPermissions logIn collectGarbage etc. > 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. While I agree, I'm a little reticent to add a feature like that when I have a feeling that Zeev and friends will be adding it themselves. However, I do see the point. more thinking to do... _alex > 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. |
From: Peter B. <re...@f2...> - 2001-05-14 07:16:07
|
At 02:14 PM 5/13/01 -0700, you wrote: >Sadly, function names are not case sensitive in php, so one has to be a >bit careful. They will be shortly - see http://www.zend.com/zend/week/week36.php I quote: TLK: variable, class and function naming Since PHP 2, the language has always regarded variable names to be case sensitive, while functions and classes are not. It appears that either PHP 4.1 or PHP 5 will break compatibility with this previous functionality, and regard everything as case sensitive. Logically, this makes more sense, and most of the core developers have already noted their excitement at the prospect. The changeover will also result in a boost in speed, since converting cases will not be necessary. For those who prefer the old way, there will be a compatibility mode .ini option. HTH! Peter. --oOo-- Narrow Gauge on the web - photos, directory and forums! http://www.narrow-gauge.co.uk --oOo-- Peter's web page - Scottish narrow gauge in 009 http://members.aol.com/reywob/ --oOo-- |
From: Andreas A. <a.a...@th...> - 2001-05-14 10:44:54
|
Hi 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". I'm with you. But there is still the problem with the extra namespace for variables from the outside. So with the underscore there is a clear distinction between globals from the outsite $myName_ (passed in the url via script.php?myName.=value) and "internal" globals like $myName. On the other hand we have a "namespace" for varibales that are, and functions that should be considered as privat: $_privateName and _privateMethod();. So I would only use the leading "_" for private methods/vars and the trailing "_" for globals from the outside. Possibly a good solution I think (and there are not to many _'s than prepending or appending to each and every variable/mehtod). Andi |