I've got a question which has been boggling me for a while. Here's an example situation:
class X {
private $yObj;
private $zObj;
public function __construct() {
$this->yObj = new Y('foo');
$this->zObj = new Z($this);
}
}
class Y {
private $var;
public function __construct($var) {
$this->var = $var;
}
public function getVar() {
return $this->var;
}
}
class Z {
public function __construct($xObj) {
// Method to grab the y object's var value..
}
}
$foo = new X();
Using dependency injection, object z has access to object x's public methods, and wants to retrieve the $var attribute that the y object has.
There are two main methods to go about this I can think of, either:
Implement an operation in class x like so:
public function getVar() {
return $this->yObj->getVar();
}
and have the y object call that method, or implement an operation in class x like so:
public function getY() {
return $this->yObj();
}
and have the y object call $xObj->getY()->getVar()
This situation keeps cropping up in my code, and I am wondering which would be the best method of implementation.
For each scenario, the class which is, for this example, labelled class y, only exposes certain operations, all the internal workings are private as expected, so it can't be abused.
Any help or discussion about this question would be greatly appreciated.
Thanks in advance!
-Sean
This post has been edited by Bladescope: 13 April 2012 - 09:34 AM

New Topic/Question
Reply



MultiQuote




|