I don’t know where this idea settled into my little head, probably back in college. I sold practically every book from my college days, keeping only a few books (For anyone who had a blue book by Blum with a picture of driftwood on it for Software Engineering, you have my sypathies. Can’t believe its still $97 bucks) so I can’t look back to see if any of my classes was the culprit (could it be java? it sure wasn’t assembler!). I have always defined get/set methods for my classes with get/set in the name.

class Card {
var $name;

function set_name($name = "") {
$this->name = $name;
}

function get_name() {
return $this->name;
}
// etc
}

I’ve seen some people do this:

class Card {
var $name;

function name($name = "") {
if (empty($name)) {
return $this->name = $name;
} else {
$this->name = $name;
}
}
// etc
}

Using the same method for getting or setting, the deciding factor is if a parameter is passed.

I mentioned this in a Codesnipers post about how Ruby does get/set for attributes. Caleb Tennis showed a way I could customize the get/set methods of Ruby so it DOES allow you to do getName or setName.

My recent language crush is Perl, and I’ve seen many modules doing it the second way.

But “name” isn’t a function so I don’t really like doing it the second method. But is it an “okay” convention that I should wave a white flag and accept? I guess I could look at it this way, if a function name is just a noun, then it’s for an get/set method, if not then it’s a regular method.

Should I change my mind on this? what do you do?