Combining getters and setters into gettersetters
In terms of "good code", is it acceptable to 开发者_开发问答combine set and get methods into one? Like this:
public function dir($dir=null) {
if (is_null($dir)) return $this->dir;
$this->dir = $dir;
}
It's pretty dreadful. It makes it impossible to set the value to null, for one thing.
In a language that distinguished undefined from null, then it would be reasonable to do this with that undefined value, rather than null.
The mismatch between something being returned or not isn't even valid in many languages. In either case, why not return it anyway, to allow reasonable chaining of x.dir = y.dir = someValue
? But that's a nick-pick, my first paragraph is my main answer.
My initial thought is no the code is harder to read and therefore maintain. I have gotten to where I either use C#'s auto property or just a public field.
This pattern is at least very common in jQuery; I'd say it is fine.
精彩评论