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;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.