I have a service class that contains a number of objects that hold different kinds of data and their respective behaviour. One of these objects has other objects that use some of the data of the first two objects...try saying that 10 times fast!
So in the example below, object3 takes arguments x and y from object1 and parameter z from object2. In turn, these parameters aren't "used" by object3, but are passed through as arguments into object3_1 and object3_2.
class ServiceClass {
protected $object1;
protected $object2;
protected $object3;
public function doSomething (){
$this->object3->calculateSomething($x, $y, $z);
}
}
class Object1Class {
protected $data = {"x"=>"y"};
public function getX (){
//return x
}
public function getY (){
//return y
}
}
class Object2Class {
protected $data = "I'm data!";
public function getData (){
//return string
}
}
class Object3Class {
protected $object3_1;
protected $object3_2;
public function calculateSomething ($x, $y, $z){
$this->object3_1->getSomething($x, $y);
$this->object3_2->getSomethingElse($x, $y, $z);
}
}
So basically i'm trying to find the best way of getting information from the service, to object3_1/3_2?
The 4 ways I've got so far are:
Pass data from objects into object3 method and then pass on to object3_1/3_2
$this->object3->calculateSomething($this->object1->getX(), $this->object1->getY(), $this->object2->getZ());
Pass object1 and object2 into object3 then either pass data to object3_1/3_2 or pass the objects again
$this->object3->calculateSomething($this->object1, $this->object2);
Create a new class that contains properties and behaviour for the required data and pass that around.
class DataClass {
protected $xy;
protected $z;
public function __contruct($xy, $z){
$this->xy = $xy;
$this->z = $z;
}
public function getX(){
// return x
};
public function getY(){
// return y
};
public function getZ(){
// return z
};
}
$this->object3->calculateSomething($DataObject);
Create an array of the data elements and pass that around.
All of the options work, it just doesn't feel right passing the same argument through multiple methods. In the contrived example above, I've only made it go 2 classes deep for the sake of brevity, but in some areas of my project data has to be passed up to 6 layers deep before it's utilised.
With option 1, the parameter list has the real chance of growing in number - not that the final calculation method is doing too many things, but just requires all these bits of data for a single calculation. Option 3 seems attractive to me but it would duplicate the behaviour code from the original objects - so then passing the objects themselves seems better...and that's the circle i've been going in for the last few days.
Is there a better way of doing this, or am I just over thinking things?