Class design approach: setters and getters

22 views
Skip to first unread message

hdo...@gmail.com

unread,
Oct 22, 2012, 4:48:20 PM10/22/12
to phalcon-...@googlegroups.com
Hello again.

Since I'm working on mail component, I already created many setter and getter methods (about 30). And there will be about 10 more (maybe much more) methods waiting to be implemented (with setters and getters).

Do you think implementing __call magic method and use conditions approach is better?

If I implement __call magic method, there will be too many conditions in it (some methods accept only string, some are string or array, some object, etc).

Or should I stop thinking it and continue and finish it first and let's think about it for next releases?

Hidayet Dogan

Nikolaos Dimopoulos

unread,
Oct 22, 2012, 4:50:48 PM10/22/12
to hdo...@gmail.com, phalcon-...@googlegroups.com
+1 for specific getters and setters.

I know for a fact that the __call is much slower than the specific get/set. In addition it will make you check for every 'allowed' call which could be expensive.

My 2 cents :)


The contents of this message may contain confidential or privileged information and is intended solely for the recipient(s). Use or distribution to and by any other party is not authorized. If you are not the intended recipient, copying, distribution or use of the contents of this information is prohibited.


--
 
 

hdo...@gmail.com

unread,
Oct 22, 2012, 4:58:28 PM10/22/12
to phalcon-...@googlegroups.com, hdo...@gmail.com
At the same time I was thinking DRY principle. I repeated too much code for every getter/setter method.

For example:

setId, setDate, setSubject, setBody, setContentType, setCharset is about same code except they send different parameters to setOptions method. setFrom, setTo, setCc, setBcc, setReplyTo is also same (again except sending different parameter to setOptions method).

Same for getters. They have about same code except 1 line (string value of variable) difference (to call getOptions method with passing options array key as string).

Andres-Gutierrez

unread,
Oct 22, 2012, 5:02:11 PM10/22/12
to hdo...@gmail.com, phalcon-...@googlegroups.com
The setters also will help to produce a more understandable API docs :)

--
 
 

Nikolaos Dimopoulos

unread,
Oct 22, 2012, 5:06:26 PM10/22/12
to Andres-Gutierrez, hdo...@gmail.com, phalcon-...@googlegroups.com
How about an internal getter setter i.e.

__getter(x, y, z)
__setter(x, y, z

that would do all the work and is invoked by the relevant setter/getter?

i.e.

private function __myget($var)
{
    $this->_options[$var];
}

and for the actual getters:

public function getId()
{
    return $this->__myget('id');
}

public function getName()
{
    return $this->__myget('name');
}


It might be a bit tricky for the setters but you get the idea.

The contents of this message may contain confidential or privileged information and is intended solely for the recipient(s). Use or distribution to and by any other party is not authorized. If you are not the intended recipient, copying, distribution or use of the contents of this information is prohibited.


--
 
 

hdo...@gmail.com

unread,
Oct 22, 2012, 5:08:10 PM10/22/12
to phalcon-...@googlegroups.com, hdo...@gmail.com, andres.g...@phalconphp.com
Yes you're right but @method can be used for documenting internal API magic methods too.


Anyway, I continue to work with separated setters/getters :D
Reply all
Reply to author
Forward
0 new messages