你最好的赌注是魔法方法
__call,见下面例如:
class test {
function __construct(){}
private function test1(){
echo "In test1", PHP_EOL;
}
private function test2(){
echo "test2", PHP_EOL;
}
protected function test3(){
return "test3" . PHP_EOL;
}
public function __call($method,$arguments) {
if(method_exists($this, $method)) {
$this->test1();
return call_user_func_array(array($this,$method),$arguments);
}
}
}
$a = new test;
$a->test2();
echo $a->test3();
/*
* Output:
* In test1
* test2
* In test1
* test3
*/
请注意,test2和test3在由于受保护和私有而被调用的上下文中不可见。如果方法是public的,上面的例子将失败。
test1不必声明为private。
更新:添加链接到ideone,添加示例与返回值。