最好的选择是魔术方法__call,请参见以下示例:
<?phpclass 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();请注意,
test2和
test3在由于
protected和而被调用的上下文中不可见
private。如果这些方法是公开的,则上面的示例将失败。
test1不必声明
private。
ideone.com示例可以在这里找到
更新 :添加到ideone的链接,添加带有返回值的示例。



