Is it a good idea to replace getter and/or setter methods with a single method handling both?
For example:
function name($what = null){
if(!$what)
return $this->name;
$this->name = $what;
}
Used like:
// get name
print $obj->name();
// set name
$obj->name('bla');
I’ve seen some frameworks do it. Is it considered a good or bad practice by the community? 😛
It appears to be more efficient, but it looks a little confusing because I’m used to getThing() and setThing() in PHP. This style reminds me of jQuery.
What you could do is create a Get and a Set for all variables in an object.
So instead of the function getName() and getAge()
You can use get(‘name’) or set(‘name’, ‘Foo Bar’);
The functions would look like this:
If you’re looking for a efficient alternative to getters and setters, this might be it.