我想知道是否可以封装一个类的方法,但是然后暴露它们在一个消费类。例如(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
最后,我希望能够做的是不能在一个控制类的上下文外部直接引用的组件。
你的意思是像C++中的朋友类的东西? http://en.wikipedia.org/wiki/Friend_class – VolkerK 2009-08-19 09:38:05
如果你隐藏了私有方法中的功能,那么你不能双方都拥有它,那么它就不能从外部调用。 如果以任何方式公开它(通过制作一个公共方法,然后调用私有方法),那么每个人都可以调用您的私有方法。 – 2009-08-19 09:40:13
上面的评论是关于PHP当然:)朋友类会工作,但PHP中没有这样的概念。 – 2009-08-19 09:44:57