Originally posted by website
well this simply doesn't fit, doing copy+paste or having many methods almost a like is exacly what OOP IS NOT about!
you should instead do get_member($member) or something like that!
I know what I am doing. ( Just about 😉 )
The reason I want to autocreate accessor methods, as stated in my previous post, was to avoid doing cut and paste. As for having many methods almost alike, normally that would be a problem because if you changed implementation you would have to rewrite the code many times. However, in this case, obviously, the methods are autocreated using the same single piece of code. A perl-ish example:
# code not tested
foreach $member (@members) {
${"Class::$member"}{SUB} = sub {
my $this = shift;
return $this->{$member};
}
}
Another reason for creating member functions, rather than having single functions
get($member);
and
set($member, $value);
, is overriding. Suppose you want to ensure that a certain member in a subclass is only set to positive integers. With autocreated member functions, this is as simple as:
function set_member($value) {
if (! ($value > 0)) die("Someone tried a non-positive value");
parent::set_member($value);
}
With simple set() and get(), you have to add conditions.
// in subclass
function set($member, $value) {
if ($member == 'member') {
// do the checking...
}
parent::set($member, $value);
}
If you had many different members with different types of check, this would rapidly grow the length and complexity of the function.
HTH. HAND.
Dave