Gemetric shape: position

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

Let's say I have a 2d geometrical shape, whose position is a 2d point


class Shape2d

   protected $_position;

   public function __construct()
     $this->_position = new Point2d( 0, 0 );

   public function getPosition()
     // return reference
     return $this->_position;

     // or return clone
     return clone $this->_position;


What would make more sense to you for the implementation of the
getPosition method?

- Returning a clone of $this->_position, so you CAN'T alter the position
- Returning a reference to $this->_position, so you CAN alter the
position directly?

I'm not sure myself. I'm gonna implement a setPosition and setPositionX
/ Y methods too. So I thought manipulating the position through
getPosition would be overkill. I'm curious about your opinions and
reasoning behind your opinion. Thanks in advance!

Re: Gemetric shape: position

amygdala wrote:
Quoted text here. Click to load it

I never return a reference to an internal value.  The class needs to be
able to maintain validity of it's members, and returning a reference to
a private member potentially breaks that.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Gemetric shape: position

Jerry Stuckle schreef:
Quoted text here. Click to load it

Yes, of course, this makes perfect sense to me. Thanks Jerry!

Re: Gemetric shape: position

On Fri, 23 Jan 2009 19:06:06 +0100, wrote:
Quoted text here. Click to load it
<snipped code>
Quoted text here. Click to load it

Why not just one setPosition?

$shape->setPosition($x, $y);

Quoted text here. Click to load it

It sounds as though having setters and getters would be appropriate
here, in which case, you're probably already on the right track.

$email = str_replace('sig.invalid', '', $from);

Site Timeline