我想知道是否可以封装一个类的方法,然后在消费类中公开它们。例如(JFTR,我知道这段代码是错误的)
class Consumer{
public function __construct($obj){
$this->obj = $obj;
}
public function doCommand(){
$this->obj->command();
}
}
class Consumed{
//I would make the constructor private, but to save space...
public function __construct(){}
private function command(){
echo "Executing command in the context of the Consumer";
}
}
$consumer = new Consumer(new Consumed);
$consumer->doCommand();
//just to reiterate, I know this throws an error
最终,我希望能够制作不能在单个控制类的上下文之外直接引用的组件。