in reply to OO Getters/Setters

Now here is where I'm confused: Why do you need a different method for each bit of data? The first thought in my mind after reading the class::accessor documention was why not do something like this:
$myObject->getValue("myDataField"); $myObject->setValue("myDataField","New Data");
Frankly you don't need to and your second set value is perfectly valid. For instance, if you are working with a custom Date object, having a setMonth, setYear and setDateOfMonth would be invaluable, but for convenience, it be great to have something like, setDate($year,$month,$day). The less typing you do, the less chance of a bug, right?

Also, if you want to make sure that two values which are tightly related and have some sorta dependency on each other, it's less-end-developer-error-prone to put the two together. For instance, if you have an interface for configuring network cards, having a setIp and setHostmask would cause less headache, since assuming a hostmask may be bad (in your eyes).

Finally, sometimes it's a requirement that they both get done. Having something like a prepared sql statement and setting the placeholders, it'd be bad design to do $sth->bind_param($p_num) and then $sth->bind_param($bind_value). Allowing one to get done w/o the other (if they existed) would be an error. $sth->bind_param($p_num, $bind_value) is an example of one that makes perfect sense to do as a dual parameter set. And binding the same parameter twice would be a strange thing to do.

The only time you shouldn't do a mass-set $object->setALotOf Values is either...
.. for convenience, where a pattern of sets occurs over and over again
.. for preventing certain patterns, since though they can be done, it'd just be silly
.. for preventing one thing from happening w/o the other since one acts upon the other and can't be seperated


Play that funky music white boy..

Replies are listed 'Best First'.
Re: Re: OO Getters/Setters
by hardburn (Abbot) on Dec 31, 2003 at 15:55 UTC

    . . . for convenience, it be great to have something like, setDate($year,$month,$day)

    Better still is to have a setDate method take a DateTime object, or one of CPAN's multitude of other Date objects, if you really want to.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      Of course, but the point is of theoretical example with something that everyone can relate to that is tightly coupled. :)

      Play that funky music white boy..