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;
}
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;
}
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.
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.