a very simple point:
class Point
{
private $x, $y;
public function __constructor($x, $y)
{
$this->x = $x;
$this->y = $y;
}
public function getX()
{
return $this->x;
}
public function getY()
{
return $this->y;
}
}
and a circle
class Circle
{
private $r;
private $point;
public function __constructor (Point $point, $r)
{
$this->point = $point;
$this->r = $r;
}
public function getPoint() // here is the law breaking
{
return $this->point;
}
}
$circle = new Circle(new Point(1,2), 10);
$circle->getPoint()->getX(); // here is the law breaking
$circle->getPoint()->getY(); // here is the law breaking
of course it breaks the Demeter's law. So let me refractor it:
class Circle
{
private $r;
private $point;
public function __constructor (Point $point, $r)
{
$this->point = $point;
$this->r = $r;
}
public function getPointX()
{
return $this->point->getX();
}
public function getPointY()
{
return $this->point->getY();
}
}
$circle = new Circle(new Point(1,2), 10);
$circle->getPointX();
$circle->getPointY();
apart from it looks better, I dont see any advantage - just two extra wrapping function. Technically I again have full access to Point
and there is no way that more methods would be added to Point
. Is it still worth to use the 2nd refractored method?