【发布时间】:2012-02-28 12:03:32
【问题描述】:
我有一些 PHP 杂乱,我想委托方法。有点像穷人的混音。
基本上我想要以下内容:
<?php
class Apprentice
{
public function magic() {
echo 'Abracadabra!';
}
}
class Sourcerer // I work magic with the source
{
private $apprentice;
public function __construct(){
$this->apprentice = new Apprentice();
}
public function __get($key) {
if (method_exists($this->apprentice, $key)) {
return $this->apprentice->{$key};
}
throw Exception("no magic left");
}
}
$source = new Sourcerer();
$source->magic();
?>
不要抛出Fatal error: Call to undefined method Sourcerer::magic() in .../test__get.php。
【问题讨论】:
标签: php class delegation