class Packer{
var $packin;
public function __construct(&$packin){
$this->packin=$packin;
}
public function __tostring(){
return "[Packer]";
}
protected function __get($k){
$funcName='get'.ucfirst($k);
if(method_exists($this,$funcName)){ //[注释a]不可用 is_callable, 因为定义了__call, 总返回true
return $this->$funcName();
}
return $this->packin->$k;
}
protected function __set($k,$args){
$funcName='set'.ucfirst($k);
if(method_exists($this,$funcName)){ //同注释a
call_user_func_array(array($this,$funcName),$args);
return;
}
$this->packin->$k=$args;
}
protected function __call($k,$args){
if(method_exists($this->packin,$k)){
return call_user_func_array(array(&$this->packin,$k),$args);
}
}
}
class Product{
var $price;
}
class Book extends Packer{
function setPrice($p){
$this->packin->price=$p;
echo("set price $p/n");
}
function setName($s){
echo("set name $s/n");
}
}
$p=new Product();
$p->price=120;
$book=new Book($p);
$book->name="c++ primer";
$book->price=130;
echo($book->price);
?>