PHP:通过静态代理调用受保护方法时的奇怪行为


PHP: weird behaviour when calling a protected method via a static proxy

我觉得这真的很奇怪。有人可以给出解释吗?

abstract class UIController{
   public static function exec($context,$vdo){
      return call_user_func(array($context, $vdo));   
   }
}
class UIControllerSettings extends UIController{
    protected function save(){
        return "saved'n";
    }
}
$controller = new UIControllerSettings();
echo UIController::exec($controller, 'save'); //<-- prints "saved"
echo $controller->save(); // <-- throws a fatal error 

不确定这是否有意义;两个调用不应该产生致命错误吗?

提前谢谢。

更新:

这是输出:

$ php --version
PHP 5.3.3-1ubuntu9.5 with Suhosin-Patch (cli) (built: May  3 2011 00:48:48) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
$ php test.php 
saved
PHP Fatal error:  Call to protected method UIControllerSettings::save() from context '' in test.php on line 17

Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes:

http://php.net/manual/en/language.oop5.visibility.php。

由于UIController::exec()是解决公共静态函数的正确方法,我的猜测是 call_use_func() 正在作为类本身内部的调用进行处理。另一方面,$controler->save()无法运行,因为它是受保护的功能。

受保护的方法可以在对象(父项和子项)的继承行中的任何位置调用。因为UIController::exec UIControllerUIControllerSettings的父代码,实际上是在调用UIControllerSettings::save而不是主代码,所以完全没问题。